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

User::findFirst(['id' => 2]) return a record with id = 1

When I try this:

User::findFirst("id = 2");

it works, but when I send array ['id' => 2] I just get first row in database. Why is this happening?



145.0k
Accepted
answer
edited Jul '18

Because there is no argument 'id' for parameters as array, you can do this fetch like this:

User::findFirst(2);
User::findFirst("id = 2");
User::findFirst([
    "id = 2",
    "columns" => "firstName" // only get firstName
]);

If you use array then first argument if it's without name will be always treated as conditions key.



1.5k

sad (((

Because there is no argument 'id' for parameters as array, you can do this fetch like this:

User::findFirst(2);
User::findFirst("id = 2");
User::findFirst([
  "id = 2",
  "columns" => "firstName" // only get firstName
]);

If you use array then first argument if it's without name will be always treated as conditions key.

User::findFirstById(2);
User::findFirst(['id = ?0', 'bind' => [2]]);