We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

PHQL Strange behavior

I have the following code that was working 2 days ago.

$arrResult = $this->modelsManager->executeQuery("SELECT * FROM Phrase p inner join User u on p.user = u.id where (p.phrase like :search: or u.codeLink2me = :search:)",
    array('search' => '%' . $searchInput . '%')
);

//getting user info
if($arrResult->valid()) 

Sudenlly it stopped working at production but the same code works at development environment. I got this error at production:

Cannot access empty property on line 32.

The line 32 is if($arrResult->valid()). This just happens if the $arrResult has a result. If it doesnt the valid() property is false and the code work as expected.

I dont have a clue of what is going on.

BTW: I dumped the production db to use for a test and everything works on development environment.



33.8k
Accepted
answer

Maybe try with a count($arrResult) to know if the PHQL finded, at least, one result.



98.9k
edited Aug '14

I think $arrResult->valid() must not be called directly, at least not in that order, it must be used this way:

$arrResult->rewind();
while ($arrResult->valid()) {
    $phrase = $arrResult->current();
    $arrResult->next();
}

However this is the same as:

foreach ($arrResult as $phrase) {
  //...
}

If you want to check if the resultset has returned any rows, you might want to follow the advice by @RompePc and use count($arrResult)