0

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); 
2
  • Can you print the result of Zend_Debug::dump($this->_db->fetchRow($select)); without calling toArray()? Commented Jun 4, 2013 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" } Commented Jun 4, 2013 at 18:50

2 Answers 2

1

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;
}
Sign up to request clarification or add additional context in comments.

3 Comments

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
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.
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);
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.