Update model acting weird

Hi guys, I’m not quite sure why this isn’t working. The code makes logical sense to me, but for some reason the save is throwing an error. Can someone help enlighten me on why this is failing?

What I am trying to do:

Check the "opened" column to see if it is null. If it is, update that column upon initial view and then reload the model.




$model = $this->loadModel();


            if($model->opened === NULL)

            {

                $model->opened = new CDbExpression('NOW()');

                

                if($model->save())

                {

                    $model = $this->loadModel();

                }

                else

                {

                    //throw new CHttpException(404,'The message could not be updated.');

                }

            }



And what is the error?

I’m not seeing an error logged, which is why I am completely confused. I have logging on for LogError and LogWarning, and I see nothing.

There is no need to reload the model, because it contains the new values.

But if you want to do it anyway, you should use


$model->refresh();

Thanks, I will do that, but the reason for the reload is that “opened” would have “NOW()” in it since it’s the expressions being handed over. I will use the refresh though, thanks for info.

And the error is on the $model-save(), I’m not even getting to this part yet… =(

Found the error. I put <?php echo CHtml::errorSummary($model); ?> as the throw statement and found that a field was sending a buggy info. Sorry for the bother guys… =D

how about using saveAttributes instead of save?




$model = $this->loadModel();

if($model->opened === NULL)

{

	if($model->saveAttributes(array('opened'=>new CDbExpression('NOW()'))))

	{

		$model->refresh();

	}else{

		//throw new CHException('The message could not be updated.');

	}

}