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

Collection::findFirst is returning a document when criteria specified does not match

I have a document:

_id: <object_id>
user_id: 123

When I use findFirst to find a document with a user id that does not exist:

 Users::findFirst(array("user_id" => 9867));

I find it returns the only document that exists when I need false. Is this expected behaviour?



34.6k
Accepted
answer

It must be an array of an array:

Users::findFirst(array(
    array("user_id" => 9867)
));


868
edited Apr '16

The problem is still exists for me.

$entity = Post::findFirst(123123); // $entity = false
$entity = Post::findFirst("id=123123"); // $entity = false
$entity = Post::findFirst(array("id" => 123123)); // $entity - first founded post in my DB
$entity = Post::findFirst(array(array("id" => 123123))); // $entity - Also first founded post in my DB

@khaxel you're mixing the way of querying data using the ORM in the ODM, please check the docs