I am using Yii afterdelete() to update the related data which is deleted in another table. Here is my code in the controller:
Controller Action
public function actionDelete($id)
{
if(Yii::app()->request->isPostRequest)
{
// we only allow deletion via POST request
$this->loadModel($id)->delete();
// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
if(!isset($_GET['ajax']))
$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
}
else
throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
}
Model function
protected function afterDelete()
{
parent::afterDelete();
$show_model = new Show();
$show_model = Show::model()->findAll('tbl_season_id='.$this->id);
$show_model->updateAll('tbl_season_id = NULL, on_season=0');
}
As @Gregor said, a good use of active record relations would make the job much easier.
So, in Show model you would have something like:
While in Season model you would have something like:
Having relations defined, will give you the ability to do this:
Hummm, but if you noticed that second line in the
afterDeletewhich callsfindByID($id)but we are inside theafterDeleteand the record is actually dead (deleted)!!To fix this you can grab the
idjust before the model is deleted using avariable& abeforeDeleteNow if you change the
idin theafterDeleteto$this->cached_season_id.. it should work.Well, this solution is based on this yii-fourm-topic and i am not quite sure if it’s going to work as it is!! So, give it a try & let us know what happens?