Beforesave Isn't Working

This is the class


abstract class TrackStarActiveRecord extends CActiveRecord

{

    protected function beforeSave()

    {

        if(Yii::app()->user !== null)

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

        else

            $id = 1;


        if($this->isNewRecord)

            $this->create_user_id = $id;


        $this->update_user_id = $id;


        return parent::beforeSave();

    }


    public function behaviors()

    {

        return array(

            'CTimestampBehavior'=>array(

                'class'=>'zii.behaviors.CTimestampBehavior',

                'createAttribute'=>'create_time',

                'updateAttribute'=>'update_time',

                'setUpdateOnCreate'=>true,

            )

        );

    }

}

behaviors is functioning fine, but beforeSave() doesn’t trigger when I’m trying to add a comment. What am I doing wrong?

If anyone is experiencing the same issue, it’s due to validation. beforeSave() isn’t triggered unless validation is successful.

In this case Comment model had create_user_id and update_user_id as required, but these values are populated only after validation is done. Modifying the rules() in Comment model fixed my problem.

Ya… But are you inserting create_user_id and update_user_id in backend…?? so you have not put this field in required.