Yii2:如何在Yii2上删除与关系3表

时间:2018-07-22 18:53:44

标签: yii2 yii2-advanced-app

我有以下表格,我想单击Pak按钮上的Delete,它可以删除所有3个表格关系。

我该如何实现?

table Pak : id_pak, pak_name/////
table Church : id_church, church_name, id_pak/////
table Member : id_member, name_member, id_church////

public function actionDelete($id)
{
    $this->findModel($id);
    $select = Church::find()
            ->select('church_name')
            ->where(['id_pak' => $id])
            ->all();

    $a3 = Church::find()
        ->select('id_church')
        ->where(['id_pak' => $id])
        ->all();

    $select2 = Member::find()
            ->select('member_name')
            ->where(['id_church'=> $a3])
            ->all();
    Church::find()->where(['id_pak' => $id])->one()->delete();
    Pak::find()->where(['id_pak' =>$id])->one()->delete();
    Member::find()->where(['id_church'=> $a3])->one()->delete();
    return $this->redirect(['index','select'=>$select,'select2'=>$select2]);
}

2 个答案:

答案 0 :(得分:0)

在InnoDB Engine中使用约束

如果您使用的是InnoDB,并且已正确添加约束on delete cascade并在模型中定义了各自的关系,则无需担心其他表中的相关记录,只需在其中查找模型即可。 Pak并将其删除。

public function actionDelete($id)
{
    $this->findModel($id)->delete();

    return $this->redirect(['index']);
}

protected function findModel( $id ) {
    if ( ($model = Pak::findOne ( $id )) !== null ) {
        return $model;
    }

    throw new NotFoundHttpException ( 'The requested page does not exist.' );
}

手动删除

或者如果您出于某种原因未使用innoDB或未使用约束,则可以在ActiveRecord模型中为beforeDelete()覆盖Pak并删除Pak的所有子行在Church中建模,并在beforeDelete()中覆盖Church,以删除Member模型中的所有子行,并从那里返回true,以继续删除{{1 }}模型

我假设您在Pak模型中定义了以下关系

Pak

以及public function getChurch(){ return $this->hasOne(Church::className(), ['id_pak'=>'id_pak']); } 模型中的以下内容

Church

然后在public function getMember(){ return $this->hasOne(Member::className(),['id_church'=>'id_church']); } 模型中覆盖beforeDelete()

Pak

并在public function beforeDelete() { $this->church->delete(); return parent::beforeDelete (); } 模型中覆盖beforeDelete()

Church

并在您的public function beforeDelete() { $this->member->delete(); return parent::beforeDelete (); } 中找到模型并调用delete

actionDelete()

还有一篇不错的文章,介绍如何在父模型here中实现public function actionDelete($id) { $this->findModel($id)->delete(); return $this->redirect(['index']); } 方法。

答案 1 :(得分:0)

以下代码可以帮助您解决问题。

public function actionDelete($id)
{
    $select = Church::find()
            ->select('church_name')
            ->where(['id_pak' => $id])
            ->all();

    $a3 = Church::find()
        ->select('id_church')
        ->where(['id_pak' => $id])
        ->all();

    $select2 = Member::find()
            ->select('member_name')
            ->where(['id_church'=> $a3])
            ->all();

    // ---------- start ---------------
    $park = Park::find()->where(['id_pak' => $id])->one();
    if ( $park->delete() ){
        Pak::deleteAll('id_pak = :id', [':id' => $id]);
        foreach ($a3 as $value) {
            Member::deleteAll('id_church = :id', [':id' => $value->id_church ]);
        }
    }
    // ---------- end ---------------

    return $this->redirect(['index','select'=>$select,'select2'=>$select2]);
}