How to check in Phalcon id valid to do fetchAllAssoc()?

34 Views Asked by At

I don't use Phalcon much and am having some trouble with some code after a migration to Phalcon v3 with code that worked before.

foreach($this->facets as $k => $facet) {
    $facets[$facet] = $results->getNext()->fetchAllAssoc();
}

The problem now is that I can see that when $results->getNext() returns an object with a null result and num_rows of 0, the chained fetchAllAssoc() gives an exception.

How can I recode this so that a fetchAllAssoc() is not done unless getNext() returned a valid result?

1

There are 1 best solutions below

0
Nick Weavers On

This is what I ended up doing. May not be a very elegant solution but it works. I'd be happy to see suggestions for improving it.

foreach($this->facets as $k => $facet) {
  $tmp_result = $results->getNext();
  $has_row_to_fetch = $results->valid();
  if (!$has_row_to_fetch) {
     break;    
  }
  $facets[$facet] = $tmp_result->fetchAllAssoc();
  //$facets[$facet] = $results->getNext()->fetchAllAssoc();
}