Cdbexpression Doesn't Set Value In Beforesave Function

Hello, i use beforeSave function to add some default values to my model but only owner_id is set. created_dt is not set and when i create a new model the error appear that "Created Dt cannot be blank.". Why owner_id is set but created_dt is not set by the CDbExpression?


protected function beforeSave()

	{

		if(parent::beforeSave()) {

			if($this->isNewRecord) {

				$this->created_dt = new CDbExpression('NOW()');

				$this->owner_id = Yii::app()->user->id;

			}

			return true;

		} else {

			return false;

		}

	}

If I’m not mistaken, beforeSave() runs after validation has finished, so validation is happening before the field is populated.

You could remove any validation rules for created_dt if that field is set automatically and cannot be edited by the user. That will at least resolve the error you’re seeing.

Thank you, the solution was to remove validation rules.

or you could use beforeValidate if you need to validate the timestamps