Fernando Ferrari
Fernando Ferrari

Reputation: 586

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?

Upvotes: 0

Views: 1048

Answers (2)

jsteinmann
jsteinmann

Reputation: 4752

wrap your query in a condition and/or throw a new exception when condition isn't met

Upvotes: 1

Richard Parnaby-King
Richard Parnaby-King

Reputation: 14882

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

Upvotes: 2

Related Questions