cakephp 3.x级联删除不起作用 [英] cakephp 3.x cascade delete not working
问题描述
我有3个表,分别是文章,注释,地址。
I have 3 tables names articles,comments,addresses.
articles -> fields(id,title,body)
comments -> fields(id,article_id,comment)
addresses-> fields(id,article_id,address)
在我的文章控制器中,我一直保持dependent => true和也是CascadeCallbacks => true。首先,我尝试使用dependent => true,我先完成工作,然后添加了层叠,但仍然无法正常工作。以下是我的代码。
and in my articles controller i have kept dependent=>true and also cascadeCallbacks=>true. First i tried with dependent => true,i dint work then added cascade, still it does not work. Below is my code.
$this->hasMany('Comments', [
'className' => 'Comments',
'dependent' => true,
'cascadeCallbacks' => true,
]);
$this->hasOne('Addresses',[
'dependent' => true,
'cascadeCallbacks' => true,
]);
但是在删除文章时,不会删除关联的记录。
but while deleting articles, associated records are not deleted.
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帮助
Please tell me what is the mistake i did. or any code need to be added or changed???? Pl help
推荐答案
尝试一下:
$ this-> hasMany ('Comments',[
'foreignKey'=>'article_id',
'dependent'=> true,
'cascadeCallbacks'=> true
]);
这篇关于cakephp 3.x级联删除不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!