且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

级联删除表的子记录

更新时间:2023-02-02 23:03:18

在您的模型中重写 beforeDelete 方法以在删除父记录之前递归删除所有子记录,即

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.

您也可以使用 CDbCommand 来执行删除操作.

You could also just use CDbCommand to perform the deletion.