Search code examples
zend-frameworkfatal-errorfetchall

Zend Framework - fetchAll returns fatal error when it has no rows returned?


I know that when I try top use fetchAll and it returns a fatal error, the reason is because it has returned no records on the query. But my question is, how to treat it? How can I know if the query will not return any records, so i do not use toArray()?

For instance,

$table = new Application_Model_Proucts();
$products = $table->fetchAll()->toArray();

How can I do a verification of the query before I put the toArray method?


Solution

  • If there are no records returned from fetchAll() then you are passing nothing to toArray(), which is where the error is occurring.

    Try wrapping the last but of your code in an if statement:

    $products = $table->fetchAll();
    
    if(count($products))
    {
      $products = $products->toArray();
    }