Yii Framework Forum: Beforesave Isn't Working - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Beforesave Isn't Working abstract class TrackStarActiveRecord extends CActiveRecord

#1 User is offline   haosmark 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 35
  • Joined: 27-January 13

Posted 19 February 2013 - 07:54 PM

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?
0

#2 User is offline   haosmark 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 35
  • Joined: 27-January 13

Posted 19 February 2013 - 08:04 PM

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.
0

#3 User is offline   Ravi Bhalodiya 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 64
  • Joined: 27-August 12
  • Location:Gujarat India

Posted 06 July 2013 - 07:04 AM

View Posthaosmark, on 19 February 2013 - 08:04 PM, said:

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.
Regards
Ravi Bhalodiya.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users