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?

1
  • You need to do some debugging. Try var_dump($result);exit; right after the fetchAll to see what $result is. Commented Dec 22, 2012 at 15:58

1 Answer 1

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

1 Comment

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); framework.zend.com/manual/1.12/en/…

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.