Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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.



20.4k
Accepted
answer

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



85.4k
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)