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?
Try this:
$post = NewsPost::findFirst("id = 1");
or
$post = NewsPost::find(
array(
"conditions" => "id = ?0",
"bind" => array(0 => 1)
)
);
I use:
$instance = Model::findFirst($id);
Where $id is a primary key.
Use
NewsPost::findFirst(['id = 1']);
or
NewsPost::findFirst(1)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With