我有 3 个表,分别为文章、评论、地址。
articles -> fields(id,title,body)
comments -> fields(id,article_id,comment)
addresses-> fields(id,article_id,address)
在我的文章控制器中,我保留了 dependent=>true 和cascadeCallbacks=>true。首先我尝试使用 dependent => true,我不起作用然后添加级联,但它仍然不起作用。下面是我的代码。
$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']);
}
请告诉我我犯了什么错误。或者需要添加或更改任何代码???
请帮忙