cascade delete the child record of the table

后端 未结 2 932
眼角桃花
眼角桃花 2021-01-14 08:59

I have table with column id, name and parentid

relation function in model:

 \'location_parent\' => array(self::BELONGS_TO, \'Location\', \'parenti         


        
2条回答
  •  走了就别回头了
    2021-01-14 09:20

    In your model override the beforeDelete method to delete all child records recursively before deleting the parent i.e.

    public function beforeDelete(){
        foreach($this->location_children as $c)
            $c->delete();
        return parent::beforeDelete();
    }
    

    Be sure to wrap the initial delete call in a transaction to ensure all or none of the records are deleted.

    You could also just use CDbCommand to perform the deletion.

提交回复
热议问题