Set attribute value to NULL

Hi folks,

I’m trying to set a NULL value to a active record attribute. I’m using $model->created_at = NULL; but it not work, if I run the query direct on database ever thing works.

how can I set a NULL value to a active record param?

and why $model->created_at = NULL; not work?

Thanks.

Try using $model->created_at = "NULL";

Koz, I already did this, but not work too.

try

$this->att = new CDbExpression(‘NULL’);

jonah,

your tip, worked fine, now I have one question… why if I set the null value direct it doesn’t work?

Thanks for all.

What is the SQL generated if you directly set $model->created_at = NULL

In general, in php:

$var = null;

is the same as similar to unset($var);

setting the variable to null and using new CDbExpression(‘NULL’); has had the same result for me in the past.

qiang where can I see the sql generated?

The problem happened just when I create a new record, when I update the record, the $model->created_at = NULL works fine.

I saw this after jonah’s post, when I did some tests.

Put this in your component configuration:


			'log'=>array(

				'routes'=>array(

					'web'=>array(

						'class'=>'CWebLogRoute',

						'levels'=>'trace, info, error, warning, application',

						'categories'=>'system.db.*, application',

						'showInFireBug'=>true //firebug only - turn off otherwise

					),

				),

			),

jonah, thanks for help me .

qiang, the query generated is below:




INSERT INTO `projects` (`name`, `client_id`, `status`) VALUES (:yp0, :yp1, :yp2)



How you can see, although I have set the field $projects->created_at = NULL, the value don’t appeared on sql generated.

Thanks

You should also enable enableParamLoggin in the ‘db’ component configuration.




   // in config/main.php


  'db'=>array(

       // All your database conf...

       'enableParamLoggin'=>true

   );




The, when you see the generated sql, you will se the params passed to (in this case) :yp0, :yp1, :yp2

This way, you (and we) can have a better idea of what is happening…

I also spent quite a few minutes on this. Setting the attribute to null works.

But I made the mistake of making the attribute a “required” field, and spent some time figuring out what’s wrong and trying the various suggestions mentioned above.