0

在我的模型中,我具有以下功能

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;
}

当它被调用时,它返回致命错误:

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

谁能指出我做错的方向。

谢谢。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"
}

尝试使用结果在控制器中填充表单,如下所示

    $userdetails = new Account_Model_User;
    $userdetails->getbyid($user->user_id);
    $userdetails = $userdetails;
    $form = new Account_Form_Profile; 
    $form->populate($userdetails); 
4

2 回答 2

1

从那个Zend_Debug::dump调用的外观来看,$this->_db->fetchRow($select)已经返回和数组,所以如果你调用toArray()它会抛出你提到的错误。

这完全取决于您希望getbyid函数返回什么,但我想说只需将您的代码更新为:

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;
}
于 2013-06-04T19:00:03.653 回答
0

fetchRow() 函数已经返回一个数组,因此,您不需要将其“强制转换”到数组。你可以通过类似的方式直接访问它

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

希望对我有帮助。

于 2013-06-05T11:20:13.987 回答