Model Search defaultOrder change makes model "empty"?

I have changed the defaultOrder in the search function of a model from one column to another one, and now I’m getting the following error when trying to call that model and trying to create a default object from it:


Creating default object from empty value

Really, the only thing I do is changing that column, and it does exist.

Here is how the model’s search function’s relevant bit looks like:




return new CActiveDataProvider($this, array(

	'criteria'=>$criteria,

		'sort'=>array(

			'defaultOrder'=>'t.create_time DESC',

		),

	)

);



And here is how I call it in the controller:




$model = Something::model()->activeStatus()->findByPk($id);

$model->modified_flag = 1; // <--- This is the line the error points to

$model->update_usr_id = Yii::app()->user->id;

$model->update_time = time();

$model->save();



Yes, [font="Courier New"]$id[/font] does exist, and is a valid primary key. Again, this all works perfectly fine if I use [font="Courier New"]create_time[/font] for the [font="Courier New"]defaultOrder[/font], but breaks (causes the above error) if I use [font="Courier New"]update_time[/font] for example.

Any ideas what this could be caused by?

EDIT #1 (Jan 13, 2016, 21:39 CET):

Here are the contents of [font="Courier New"]activeStatus()[/font] (this is added to my base model and is used very often throughout the site when calling models):




public function activeStatus()

{

	$this->getDbCriteria()->mergeWith(array(

		'condition' =>'t.status_id=:status_id',  

		'params'=>array(

				':status_id'=>Types::$status['active']['id'],

		), 

	));  

	return $this; 

} 



EDIT #2 (Jan 13, 2016, 21:59 CET):

Ok, I thought I was adding sufficient information to find the root cause of this, but apparently I wasn’t.

The summarise what I’m doing: the function in the controller is an action serving an ajax call. I pass the [font=“Courier New”]model[/font] and [font=“Courier New”]id[/font] variables over from the call when a check box is checked in a list. Again, this works perfectly fine when the column in defaultOrder is create_time, but when I change this to update_time it gives the error.

What we know for sure is that some of the records in the db have null for update_time, but normally that should just mean they come to the beginning of the list (in case of DESC), but I see no reason why that would make $model to become null.

Where ‘Creating default object from empty value’ is fires? What line?

Can you post code where error is fired?

It means that $model is null.

Have you checked if $model is null?

Well, that is exactly my point, you see? Why would [font="Courier New"]$model[/font] become [font="Courier New"]null[/font] just by changing [font="Courier New"]defaultOrder[/font], can you explain me that?

Check if $model is null after this line:




$model = Something::model()->activeStatus()->findByPk($id);



Here I don’t see that ActiveDataProvider is used.

I don’t think I need [font=“Courier New”]ActiveDataProvider[/font] here, I’m not using it in a list, and I believe the way I call it there should be calling it correctly without it, but my question is, again, why does it work with one but with the other column in [font=“Courier New”]defaultOrder[/font] (this, by the way, or so I would think, proves that [font=“Courier New”]ActiveDataProvider[/font] is not needed, at least it does to me)?

I don’t undestart what is the link between other column in defaultOrder and the error,

because the error is fired by a null object.

As suggested by Fabrizio the value of $model is probably "null" at the time of the error.

Can you post the code of activeStatus() ?




public function activeStatus()

{

	$this->getDbCriteria()->mergeWith(array(

		'condition' =>'t.status_id=:status_id',  

		'params'=>array(

				':status_id'=>Types::$status['active']['id'],

		), 

	));  

	return $this; 

} 



I also added it as an edit to my original post. I didn’t add it at first as I didn’t think it was relevant.

What baffles me is that, really, the only thing I change is the [font="Courier New"]defaultOrder[/font] column name, and in one case there is absolutely no issue, and in the other one it makes the model [font="Courier New"]null[/font], and causes the error. How can this be possible?

Ok, I thought I was adding sufficient information to find the root cause of this, but apparently I wasn’t.

The summarise what I’m doing: the function in the controller is an action serving an ajax call. I pass the [font=“Courier New”]model[/font] and [font=“Courier New”]id[/font] variables over from the call when a check box is checked in a list. Again, this works perfectly fine when the column in [font=“Courier New”]defaultOrder[/font] is [font=“Courier New”]create_time[/font], but when I change this to [font=“Courier New”]update_time[/font] it gives the error.

What we know for sure is that some of the records in the db have null for [font="Courier New"]update_time[/font], but normally that should just mean they come to the beginning of the list (in case of DESC), but I see no reason why that would make [font="Courier New"]$model[/font] to become null.

Two more things:

  1. Can you post content of "activeStatus" that it is in



$model = Something::model()->activeStatus()->findByPk($id);



  1. Why do you think that problem is in default order if $model from



$model = Something::model()->activeStatus()->findByPk($id);



is null?

  1. Just posted in a previous reply and also added to my original post.

  2. Because I’m getting no error if [font=“Courier New”]defaultOrder[/font] has [font=“Courier New”]create_time[/font].

Try to remove activeStatus(), so:




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



Then check in your db if records exists with




SELECT * FROM Something Where <primary_key> = $id



It was [font="Courier New"]activeStatus[/font]! Thank you both for the suggestions!

Any idea why?

Maybe this condition is not verified:




array(

                'condition' =>'t.status_id=:status_id',  

                'params'=>array(

                                ':status_id'=>Types::$status['active']['id'],

                ), 

        ));  



Do you mean this condition is not met? I checked, and the records in question all exist in the db and are active (have [font=“Courier New”]status_id = 2[/font], which equals to [font=“Courier New”]Types::$status[‘active’][‘id’][/font]).

The good thing is that I don’t need to check for the status in this list, but even if I had to, I can do it when I’m calling the actual list, not in the function I added it to, so this is really the solution to my situation, it’s just strange…

Have you tried to post in the code value of Types::$status[‘active’][‘id’] ?

At this level:




...

...

var_dump(Types::$status['active']['id']);

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

...

...



Just tried, it gives me [font="Courier New"]int(2)[/font] which is correct.