cakephp 3.x级联删除不起作用

时间:2016-08-25 14:53:27

标签: cakephp-3.0 cascading-deletes

我有3个表名称文章,评论,地址。

articles -> fields(id,title,body)
comments -> fields(id,article_id,comment)
addresses-> fields(id,article_id,address)

在我的文章控制器中,我保持依赖=> true以及cascadeCallbacks => true。首先我尝试使用dependent =>是的,我工作然后添加级联,仍然它不起作用。以下是我的代码。

    $this->hasMany('Comments', [
        'className' => 'Comments',
        'dependent' => true,
        'cascadeCallbacks' => true,
    ]);

    $this->hasOne('Addresses',[
        'dependent' => true,
        'cascadeCallbacks' => true,
    ]);

但删除文章时,不会删除相关记录。

public function delete($id = null)
{
    $this->request->allowMethod(['post', 'delete']);
    $article = $this->Articles->get($id);
    if ($this->Articles->delete($article)) {
        $this->Flash->success(__('The article has been deleted.'));
    } else {
        $this->Flash->error(__('The article could not be deleted. Please, try again.'));
    }

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

请告诉我我做错了什么。或者需要添加或更改任何代码???? Pl帮助

1 个答案:

答案 0 :(得分:3)

试试这个:     [ActivePlan]