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.
koz
(Koz)
July 5, 2010, 6:00pm
2
Try using $model->created_at = "NULL";
Koz, I already did this, but not work too.
jonah
(Poppitypop)
July 5, 2010, 6:43pm
4
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.
qiang
(Qiang Xue)
July 5, 2010, 8:06pm
6
What is the SQL generated if you directly set $model->created_at = NULL
jonah
(Poppitypop)
July 5, 2010, 9:04pm
7
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.
jonah
(Poppitypop)
July 6, 2010, 11:17pm
10
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
cyberpol
(Cyberpol 777)
July 7, 2010, 12:27pm
12
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…
adbie
(Firec)
September 3, 2010, 4:01pm
13
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.