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.

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?



133.2k
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.



256

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]]);