0

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?

J. Steen
  • 15,470
  • 15
  • 56
  • 63
user1414979
  • 92
  • 4
  • 11

1 Answers1

5

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);
Alex
  • 6,441
  • 2
  • 25
  • 26
  • Perhaps it is better to use is_object($data) in if().. and ZF TDG have method to find record by id(primary key): $obj->find($id); http://framework.zend.com/manual/1.12/en/zend.db.table.html#zend.db.table.find – tasmaniski Dec 24 '12 at 09:42