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) "bjh497@hotmail.com"
["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); 
Barry Hamilton
  • 943
  • 3
  • 15
  • 35
  • Can you print the result of `Zend_Debug::dump($this->_db->fetchRow($select));` without calling `toArray()`? – Rolando Isidoro Jun 04 '13 at 18:31
  • Zend_Debug::dump($this->_db->fetchRow($select)); array(11) { ["user_id"] => string(1) "1" ["role"] => string(13) "administrator" ["email"] => string(18) "bjh497@hotmail.com" ["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 04 '13 at 18:50

2 Answers2

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;
}
Rolando Isidoro
  • 4,983
  • 2
  • 31
  • 43
  • 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 04 '13 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](http://stackoverflow.com/questions/1301889/zend-forms-populate-and-setdefaults). – Rolando Isidoro Jun 04 '13 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 04 '13 at 22:47
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.

timmz
  • 2,194
  • 3
  • 23
  • 29