Find entity by id column, \Phalcon\Mvc\Model::findFirst() gives incorrect result

avasin picture avasin · Dec 18, 2012 · Viewed 8.5k times · Source

Currently i have table with posts, each posts has an id.

For a moment, only one posts exists, with id id = 92.

if i execute following code, i will get not false, but post with id=92:

$post = NewsPost::findFirst(['id' => 1]);
var_dump($post->id); // gives 92 

Seems to be very strange logic.. What method could be used to retrieve post by id, and that will return false/throw exception if there is no such entity?

Answer

Nikolaos Dimopoulos picture Nikolaos Dimopoulos · Dec 18, 2012

Try this:

$post = NewsPost::findFirst("id = 1");

or

$post = NewsPost::find(
    array(
        "conditions" => "id = ?0",
        "bind"       => array(0 => 1)
    )
);