findByPk(null) returns an object

Hello,

Here is my code :


function __construct($id=null) {

echo 'id : '.$id.'<br />';

$model = SquareDiagram::model()->findByPK($id);

if (!is_null($model)) {

echo 'Model exists';

}

else

{

echo 'Model doesnt exist';

}

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 ?

Thanks

Sounds like the default behavior of PHP’s stdClass.




// Try this

echo ($model instanceof stdClass)?'Y':'N';



Yii conveniently returns null if a records was not found. I usually just do something like this.




$model = SquareDiagram::model->findByPk($id);


if ($model)

{


}

else

{


}



Cheers,

Matt

Hello,

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

Thanks!

null is a legitimate value even for a primary key… I suppose the generated SQL is something like


SELECT * from table where id IS NULL

… so sometime you can get a result from this query…

If you don’t want to make a query when id is null… then just test for that condition before doing the query…

Ok, I didn’t know that ! (But the results I get doesn’t have a null primary key)

I was just wondering if it was a regular behavior or not, so if it is, there is no problem :)

Thanks

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…

Actually, I have something like this :


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.

function __construct($id=null) {

    &#036;model = SquareDiagram::model()-&gt;findByPK(&#036;id);


    if (&#33;is_null(&#036;id) &amp;&amp; &#33;is_null(&#036;model)) {


   	echo 'Model Exits';


    } else {


        echo 'Model Does not Exits';


    }


}

it check Both Wether ID null !/

plz Check Database having value

Or When creating

Instance of SquareDiagram class then Constructor Called properly and passed Argument value should be null and white space(blank)

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 :P) - 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.