Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] Trying to get property of non-object
#1

[eluser]Brandt1981[/eluser]
Hello,

The textbox will have no value when the page is loaded, but when a query is done, the textbox value should be set to the database value returned. I am using Doctrine.

I need help with the following code:

Controller home.php:

Code:
class Home extends Controller {
        
        public function __construct() {
            parent::Controller();
            
            $this->load->helper(array('form','url'));
            $this->load->library('form_validation');
        }

        public function index() {
            
            $vars['employee']='';
            $vars['employment']='';
            $vars['performance']='';
            $vars['timeoff']='';
            $vars['salary']='';
            
            $this->load->view('home',$vars);
        }    
        
        
        public function query() {
            $user_id = $this->input->post('query');
            
            $vars['employee'] = Doctrine::getTable('Employee_info')->find($user_id);
            $vars['employment'] = Doctrine::getTable('Employment_info')->find($user_id);
            $vars['performance'] = Doctrine::getTable('Employee_performance')->find($user_id);
            $vars['timeoff'] = Doctrine::getTable('Employee_time_off')->find($user_id);
            $vars['salary'] = Doctrine::getTable('Salary')->find($user_id);
            
            
            $this->load->view('home', $vars);
            
            
        }




View home.php:

Code:
<?php  echo form_open('home/query'); echo form_input('query', 'ID#'); echo form_close(); ?>

<?php echo form_open('home/query'); ?>
<p>
<label for="username">Employee ID: </label><br/>
&lt;?php echo form_input('empid',set_value('empid',$employee->pk_emp_id)); ?&gt;
</p>


Hope you can help!
#2

[eluser]kaejiavo[/eluser]
Hi,

you can use doctrine hydrate array (HYDRATE_ARRAY) to achieve this.
Code:
$employee['pk_emp_id']
will give you no trouble.
#3

[eluser]Brandt1981[/eluser]
ok. I read the link, but don't understand (I'm also tired)..i'm not much of a coder. Would you be able to show me using my code from above?

EDIT:

I changed it to this:

Code:
<p>
<label for="username">Employee ID: </label><br/>
&lt;?php echo form_input('empid',set_value('empid',$employee['pk_emp_id'])); ?&gt;
</p>

And now get this error:

Message: Uninitialized string offset: 0
#4

[eluser]kaejiavo[/eluser]
Try it like this:
Code:
//Controller
        public function query() {
            $user_id = $this->input->post('query');

//i have assumed that pk_emp_id is your table key, you maybe have to correct that
$q = Doctrine_Core::getTable('Employee_info')
    ->createQuery('e')
    ->where('e.pk_emp_id = ?', $user_id);

$employee = $q->fetchOne(array(), Doctrine_Core::HYDRATE_ARRAY);

            
            $vars['employee'] = $employee;
            $vars['employment'] = Doctrine::getTable('Employment_info')->find($user_id);
            $vars['performance'] = Doctrine::getTable('Employee_performance')->find($user_id);
            $vars['timeoff'] = Doctrine::getTable('Employee_time_off')->find($user_id);
            $vars['salary'] = Doctrine::getTable('Salary')->find($user_id);
            
            
            $this->load->view('home', $vars);
            
            
        }

//view
...
&lt;?php echo form_input('empid',set_value('empid',$employee['pk_emp_id'])); ?&gt;
...

As i dont know what you really want to do with your code, this is possibly not the best way to do it with doctrine.
It is just a way to get rid of the error you get by accessing empty object variables.
#5

[eluser]Brandt1981[/eluser]
It needs to to be in the index function, as the code works fine when there is a post to query. Only when the page first loads and the text boxes are empty is when I get the errors..
#6

[eluser]kaejiavo[/eluser]
[quote author="Brandt1981" date="1287400495"]It needs to to be in the index function, as the code works fine when there is a post to query. Only when the page first loads and the text boxes are empty is when I get the errors..[/quote]

Yes, you are right.
In the index() function you set your variables to '' so $employee is a string. In your view you try to access a property $employee->pk_emp_id which is simply not there.
#7

[eluser]Brandt1981[/eluser]
Thank you. This worked well for me.




Theme © iAndrew 2016 - Forum software by © MyBB