2

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.

pbond
  • 1,888
  • 4
  • 19
  • 35
  • Do you have the reverse association: Action HABTM Item? Where are you deleting from? You try to delete through a save operation (with nothing in the post for Action) or with Model::delete(), Model::deleteAll() ? – Borislav Sabev May 08 '12 at 08:01
  • Yes, in both Models I have declared the HABTM relation with 'unique' set to true. I indeed try to delete through a save operation, with nothing in the post for Action. So I don't understand it, with nothing in the post for Action, it maintains the records, which it should not. – pbond May 08 '12 at 14:31
  • 2
    My suggestion is to ditch HABTM relationships and go with 'has many through' relationships. – Wylie May 08 '12 at 18:35
  • I usually do that yes, since I often need a third column. But in this case, all I need is to couple to the two tables with their IDs, nothing more nothing less. But shouldn't it work like this? When I don't post any Action it should delete all existing data in the join table right? Yet it maintains the data. – pbond May 08 '12 at 19:03

1 Answers1

4

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.

Borislav Sabev
  • 4,776
  • 1
  • 24
  • 30
  • Thanks! This works. I've added the `if(empty($this->request->data['modelname']))` check to my controller, and added an empty array as proposed, before saving. I am using the HTML form helper btw. – pbond May 09 '12 at 18:15
  • So if you're using the form helper than this might be a bug or impovemnt, you should consider reporting it: http://cakephp.lighthouseapp.com/ – Borislav Sabev Jun 22 '12 at 09:22