Search code examples
zend-frameworktoarray

Fatal error when I am using toArray() in zend framework


I need help to remove fatal error of toArray() in zend framework.

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

I am using following code in my controller

$obj     = new Admin_Model_UserMapper();
$where   = array('id = ?'=>$decryptId);
$data    = $obj->fetchAll($where);
//  $currentData = $data->current();
$dataArr = $data->toArray();


$form = new Admin_Form_UserForm();
$form->setAction('edit-user');
$form->populate($dataArr);

I am getting fatal error in both condition when I use toArray() or current().

I have already used following code, but not getting any solution and it produces the same error:

$db = Zend_Db_Table::getDefaultAdapter();
$select = $db->select()->from('user')->where('id= ?',$decryptId);
$stmt = $select->query();
$result = $stmt->fetchAll();
if(count($result) > 0){
    $dataArr = $result->toArray();
} 

How can I resolve this?


Solution

  • Your fetchAll returns no data. Wrap it in a condition...

    $where = array('id = ?'=>$decryptId);
    $data  = $obj->fetchAll($where);
    if ($data->count()){
        //  $currentData = $data->current();
        $dataArr = $data->toArray();
    }else{
        // no records found!
    }
    

    The problem is with your $where, you cannot use it like key/value array. Use code below:

    $where = $this->getAdapter()->quoteInto('id = ?', $decriptId);