Hello All,
Sometimes we want to delete parent table on parent child relation on Yii,
First we must set relation,
Parent Model :
public function relations()
{
return array(
'Childs' => array(self::HAS_MANY, 'ChildModel', 'parent_id'),
);
}
Child Model :
public function relations()
{
return array(
'Parent' => array(self::BELONGS_TO, 'ParentModel', 'parent_id'),
);
}
On delete parent, at function below (parent model) :
public function beforeDelete(){
foreach($this->Childs as $c)
$c->delete()
return parent::beforeDelete();
}
That's all.
Hope this post can help you.
No comments:
Post a Comment