When I create an object like that new Object(), id should be null, and the echo of the id returns “id : <br />”, but then, ‘Model exists’ is returned, so even if id is null, findByPk finds an object. (but sometimes, for no apparent reason (the same code in the same conditions), ‘Model doesnt exist’ is returned)
Is it a bug ? Or is it normal ? Or am I doing something wrong ?
I think you didn’t really get what I try to explain (English is not my mother tongue so I am gonna try to say it better)
findByPk($id) returns an instance of the CActiveRecord, or null if there are no record found. But when I call findByPk(null), the method doesn’t return always null, sometimes, it returns an instance of the CActiceRecord (SquareDiagram in my example)
For example, if I use your code sample, it would go on the if($model), even if $id is null, and $model would really be an instance of SquareDiagram. But this is very random, when I call findByPk(null), I got either null, either a random SquareDiagram (not the same every time)
Actually, I did more complete test to see that, but I tried to make it simple here
I am not looking for a solution here (I added a “!is_null($id)” in my if), I am just wondering if it’s the regular behavior of Yii
Well… I tryed your code on my table (id, name)… at all times when the $id is null I’m getting “model doesn’t exist”… and as you wrote sometime you get the model… sometime not… so you should check a bit better the value of $id and the model returned…
If you have time… check the value of $model when you get "model exists"… try with print_r($model)… could be that there is a record with a null primary key… or that the $id is not null at all
Edit: note that $id can be 0 or an empty string,too… and your echo would be empty…
function __construct($id=null) {
$model = SquareDiagram::model()->findByPK($id);
if (!is_null($id) && !is_null($model)) {
$this->model = $model;
$this->North = new BHand($model->id_hand_north);
$this->East = new BHand($model->id_hand_east);
$this->South = new BHand($model->id_hand_south);
$this->West = new BHand($model->id_hand_west);
} else {
$this->model = new SquareDiagram();
$this->North = new BHand();
$this->East = new BHand();
$this->South = new BHand();
$this->West = new BHand();
}
}
$maniement->BSquare = new BSquareDiagram();
And I call the function without any parameter, so I am pretty sure that $id is really null, and not just empty.
And I checked the returned models, and they have an id, and it is a correct one, and it is almost never the same models (even if I didn’t really checked more than that…)
But that’s okay, I added the “!is_null($id)” in my if
I just found this post via google. I am experiencing the same issue. When I pass findByPk the value NULL it returns a result with a Pk of 4, which happens to be the model that was just created and saved to the DB directly before findByPk was called with the NULL value. I think this is a real bug. I don’t want to report it as a bug unless somebody with more experience with Yii than I have can confirm that this is not how Yii is supposed to behave.
It is kind of a big deal to me because I am practicing unit testing, and a value of NULL for a Pk when there is no data in the DB containing a NULL pk should not show the test as passing.
The "bug" should be pretty repeatable, I am running the last set of code in chapter 5 of the Yii Agile Web Application development book, my setup is PHP 5.3.1 on XAMPP with Yii release yii-1.1.8.r3324. I notice the problem specifically in the testRead method from that chapter.
I ran into the same issue, with the comment that this would happen after an insert on the same table.
After doing some debugging on the framework code, looking at code.google.com slash p/yii/issues/detail?id=969#c1 and testing with my code, it seems that this is a MySQL bug (or feature ) - searching for a null primary key after an insert returns the last inserted record. At first, I thought this would be a PDO bug, but I’ve tested the same scenario (insert and select a null pk) in the MySQL CLI - and to my hallucination I got the same result!
To me, this is a very dangerous and stupid bug, but nonetheless I’m a bit relieved this is not a Yii bug.
The solution: ALWAYS test for null/empty string before searching by PK.