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

check if FindFirst is empty

What's the correct way to check if findFirst actually found a result?

With normal find it's easy:

$meeting = Meetings::find($id);
if(count($meeting) !== 1) {
        $this->flash->error("Couldn't find your meeting. What's up?");
}  

But when using findFirst, count always returns 1

$meeting = Meetings::findFirst($id);
if(count($meeting) !== 1) { // <--- THIS IS ALWAYS 1
        $this->flash->error("Couldn't find your meeting. What's up?");
} 


98.9k

findFirst returns false if it didn't find any record, just do this:

$meeting = Meetings::findFirst($id);
if (!$meeting) {
        $this->flash->error("Couldn't find your meeting. What's up?");
} 


15.1k

Ach, thanks. That's a great tip!