Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

model

phalcon

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?

like image 687
avasin Avatar asked Dec 18 '12 02:12

avasin


4 Answers

Try this:

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

or

$post = NewsPost::find(
    array(
        "conditions" => "id = ?0",
        "bind"       => array(0 => 1)
    )
);
like image 79
Nikolaos Dimopoulos Avatar answered Oct 19 '22 00:10

Nikolaos Dimopoulos


I use:

$instance = Model::findFirst($id);

Where $id is a primary key.

like image 35
starsinmypockets Avatar answered Oct 18 '22 23:10

starsinmypockets


Use

NewsPost::findFirst(['id = 1']);

or

NewsPost::findFirst(1)
like image 2
小灰灰 Avatar answered Oct 18 '22 22:10

小灰灰


You should use:

NewsPost::findByid(1);

Where 'id' can be replaced by any of your model's properties. For example:

NewsPost::findByDescription('Description');
NewsPost::findByyourprop(yourpropval);

You can then count() the return value count($result) to determine if you received any records.

Note: I have also found the string searches to be case in-sensitive.

like image 1
Jesse Q Avatar answered Oct 18 '22 22:10

Jesse Q