Cakephp - HABTM record won't delete with unique set to true

淺唱寂寞╮ 提交于 2019-12-10 10:58:44

问题


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.

The situation is as followed:

Deletion works fine upon save when I delete 1 or more records, if ATLEAST 1 record stays 'alive'

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.

I've made sure with debug($this->request->data) right before save that it contained Nothing of Action.


回答1:


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);

Your POST should contain a 'Action' key:

array(
    'Item' => array(
        'id' => '3'
    ),
    'Action' => array(
        'Action' => array()
    )

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.

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:

  1. Put a hidden input with JavaScript

  2. 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 may be kind of intrusive, but It'll do the job.



来源:https://stackoverflow.com/questions/10484778/cakephp-habtm-record-wont-delete-with-unique-set-to-true

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!