Cakephp-HABTM记录不会被删除且唯一性设置为true [英] Cakephp - HABTM record won't delete with unique set to true
问题描述
CakePHP的默认值unique设置为true,我对此进行了编码以确保同样。
因此,我具有以下数据库结构:
项目HABTM操作,其唯一性设置为true。
CakePHP has the default of unique set to true, I've coded it just to make sure as well. So I have the following DB structure: Item HABTM Action, with unique set to true.
情况如下:
如果ATLEAST 1记录保持活动状态,则删除1条或多条记录时,删除后可以正常工作
Deletion works fine upon save when I delete 1 or more records, if ATLEAST 1 record stays 'alive'
当我删除所有记录(或1条记录,如果那是唯一一条)时,在保存时将无法工作。
Deletion doesn't work upon save when I delete all records ( or 1 record, if that's the only one). It simply keeps all existing records that way.
我已经确保在不包含任何动作的情况下使用debug($ this-> request-> data)进行调试。 。
I've made sure with debug($this->request->data) right before save that it contained Nothing of Action.
推荐答案
从您对帖子的评论中的讨论中,我仍然感到困惑,但据我了解,您正在尝试至:
以类似以下格式保存POST:
array(
'Item'=> array(
'id'=>'3'
)
使用:
From the discussion in the comments on your post I am still confused, but what I understand is that you're trying to: Save a POST in a format similar to this: array( 'Item' => array( 'id' => '3' ) With:
$this->Item->SaveAll($this->request->data);
您的POST应包含操作键:
Your POST should contain a 'Action' key:
array(
'Item' => array(
'id' => '3'
),
'Action' => array(
'Action' => array()
)
设置了动作键后,Cake知道它必须移过该关系并且是魔术。
然后它将删除。
When the "Action" key is set, Cake knows that it has to go "over" the relation and to it's magic. Then it will delete.
我刚刚测试过在我正在开发的应用中发现实际上是一个错误。 :D问题
是,对于特定的HATBM关系,我必须使用手动生成的表单,因此在不包含任何内容的发布中,未设置关系键,并且未删除记录。我想表单助手可以解决这个问题。
I just tested this on a App that I'm developing and actually found it to be a bug. :D The problem was that for the specific HATBM relation I had to use manually generated forms and thus when posting with nothing included the relation key was not set and the records, weren't deleted. I suppose that the Form Helper deals with this.
如果您遇到类似的问题,可以用两种不同的方式来管理它:
If you have a similar problem you can manage this in two different manners:
-
使用JavaScript放置隐藏的输入
Put a hidden input with JavaScript
在Controller中,检查 Action键是否为设置,如果没有添加(作为一个空数组):
In the Controller check if the 'Action' key is set, and if not add it (as an empty array):
$ this-> request-> data ['Action'] ['Action'] = array( );
$this->request->data['Action']['Action'] = array();
这可能是一种侵入性,但可以完成工作。
This may be kind of intrusive, but It'll do the job.
这篇关于Cakephp-HABTM记录不会被删除且唯一性设置为true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!