Cascade delete parent child relation on Yii

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

Featured Post

Install gammu on centos 7

To install Gammu on CentOS 7, follow these steps: 1. Update the system: Bash sudo yum update 2. Install dependencies: Bash sudo yum install...