sasori
sasori

Reputation: 5445

how to implement afterSave method to a specific controller action only?

let's say i have model A..then this model A is being used by many controllers... now I want to implement the afterSave method, only in one of the controllers that uses model A . e.g in Controller C it calls the save() function, so I want the afterSave to be called in that function only.how is that ?

protected function afterSave()
{
    parent::afterSave();
    if($this->isNewRecord)
    {
        echo "hello";
        exit;
    }
}

BECAUSE: afterSave() affects all the save() call of all the controllers that uses the Model A

Upvotes: 0

Views: 2119

Answers (2)

soju
soju

Reputation: 25312

You can try this in you afterSave function :

if (Yii::app()->controller->id!=='yourcontroller')
{
  // do what you want
}

If needed, you can also test value of Yii::app()->controller->action->id.

EDIT : or take a look at Jelle de Fries answer.

Upvotes: 2

Jelle de Fries
Jelle de Fries

Reputation: 875

I don't get why you would need an afterSave() method for this. In your action you are calling $model->save(). Can't you just do what you have to do after calling this? like so:

public function actionMyAction(){
    $model=new myModel;
    $model->attribute = 5;
    $model->save();
    $model->doLogicAfterSave(); //<-this
    $this->render('myView',array(
        'model'=>$model,
    ));
}

Since it's only for 1 controller.

Upvotes: 2

Related Questions