Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

In my model I have the following function

protected $_users ='users'; 

public function getbyid($user_id)
{
$select = $this->_db
            ->select()
            ->from($this->_users)
            ->where('users.user_id =?', $user_id);


$result = $this->_db->fetchRow($select)->toArray();


return $result;
}

When it is called it returns fatal error :

Call to a member function toArray() on a non-object

Can anyone point in the direction of what I am doing wrong.

Thanks. output of Zend_Debug::dump($this->_db->fetchRow($select));

array(11) {
["user_id"] => string(1) "1"
["role"] => string(13) "administrator"
["email"] => string(18) "[email protected]"
["password"] => string(40) "62bb49da919f0d349ed2cbbec559d7ed649dd238"
["created"] => string(19) "2013-05-09 07:34:00"
["modified"] => NULL
["status"] => string(6) "active"
["salt"] => string(40) "ce8d96d579d389e783f95b3772785783ea1a9854"
["lastlogin"] => NULL
["first_name"] => string(3) "Bob"
["last_name"] => string(5) "Smith"
}

Trying to use the result to populate a form in controller as follows

    $userdetails = new Account_Model_User;
    $userdetails->getbyid($user->user_id);
    $userdetails = $userdetails;
    $form = new Account_Form_Profile; 
    $form->populate($userdetails); 
share|improve this question
 
Can you print the result of Zend_Debug::dump($this->_db->fetchRow($select)); without calling toArray()? –  Rolando Isidoro Jun 4 at 18:31
 
Zend_Debug::dump($this->_db->fetchRow($select)); array(11) { ["user_id"] => string(1) "1" ["role"] => string(13) "administrator" ["email"] => string(18) "[email protected]" ["password"] => string(40) "62bb49da919f0d349ed2cbbec559d7ed649dd238" ["created"] => string(19) "2013-05-09 07:34:00" ["modified"] => NULL ["status"] => string(6) "active" ["salt"] => string(40) "ce8d96d579d389e783f95b3772785783ea1a9854" ["lastlogin"] => NULL ["first_name"] => string(3) "Bob" ["last_name"] => string(5) "Smith" } –  Barry Hamilton Jun 4 at 18:50

2 Answers

up vote 1 down vote accepted

From the looks of that Zend_Debug::dump call, $this->_db->fetchRow($select) already returns and array, so if you call toArray() it will throw the error you mentioned.

It all depends on what you want your getbyid function to return, but I'd say to simply update your code to:

protected $_users ='users'; 

public function getbyid($user_id)
{
    $select = $this->_db
                   ->select()
                   ->from($this->_users)
                   ->where('users.user_id =?', $user_id);

    $result = $this->_db->fetchRow($select);

    return $result;
}
share|improve this answer
 
I'm trying to use the data to populate a form, as you mentioned fetchrow returns an array. when I populate using the returned result in controller it fails with error :- Argument 1 passed to Zend_Form::populate() must be an array, object given –  Barry Hamilton Jun 4 at 20:39
 
Update your question with the code where you instantiate the form, call getbyid and use $result on Zend_Form::populate(). Btw, are you sure that the array keys from getbyid match the fields in the form you're trying to populate? Take a look at this question and its answers. –  Rolando Isidoro Jun 4 at 21:19
 
cheers rolando, sometimes all you need is someone to make you go through the steps. $userdetails->getbyid($user->user_id); should have been $userdetails = $userdetails->getbyid($user->user_id); –  Barry Hamilton Jun 4 at 22:47

fetchRow() function return already an array so, you don't need to "cast it " to array. and you can access it directly by something like

 $result = $this->_db->fetchRow($select);
//so now you can access or assign values to a variable 
$user_id=$result['user_id'];

Hope it my help.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.