0

This is my code:

        $charities = explode(',',$this->data['Charity']['charities']);
        foreach ($charities as $key=>$charity){
            $data['Charity'][$key] = $charity;
        }
        $this->Grouping->id = $this->data['Charity']['grouping_id'];
        if ($this->Grouping->save($data)){
            //Great!
        }else{
            //Oh dear
        }

The code is from an action that gets called by AJAX. $this->data['Charity']['charities'] is a comma separated list of ids for the Charity model, which HABTM Grouping.

The code works fine, except that Cake doesn't seem to check whether that charity is already associated with that grouping, so I end up with lots of duplicate entries in the join table. I can see that this will be a pain later on so I'd like to get it right now.

What am I doing wrong?

Thanks

Will
  • 1,893
  • 4
  • 29
  • 42
  • I think you may have to clear the table of any existing HABTM records for that model before you save. You could put that little snipper in the beforeSave method – JohnP Mar 17 '11 at 16:26

1 Answers1

0

A few things I spotted:

A. Is $this->data['Charity']['charities'] an array with named keys corresponding to the table columns? Because inside the foreach() loop, you are taking the key and putting it in the $data['Charity'] array. The result could be a wrong format. The $data array for Model saves is commonly formatted as $data['Charity']['NAME_OF_COLUMN1], $data['Charity']['NAME_OF_COLUM2], and so on for each column that wants to be saved. So, if the keys are numbers, then you could be having something like: $data['Charity'][0], $data['Charity'][1], which is wrong.

B. HABTM Associations are saved differently. What I do recommend, is that you take a look at the book's section on saving HABTM. I save a HABTM relation like this (supposing a relation between users and coupons):

$this->data['Coupon'] = array('id' => 1, 'code' = 'BlaBla');
$this->data['User'] = array('id' => 33);
$this->Coupon->save($this->data, false);

So, as you can see, the $data array has a subarray named after the foreign model ('User') with the columns+values that I want to save. Then I save this array to the Coupon model.

C. Be aware that CakePHP will always delete old records and insert new ones within the join table when it is 'updating' HABTM relations. To avoid this, you set the 'unique' field to false in the Model's $HABTM configuration.

YOMorales
  • 1,926
  • 1
  • 15
  • 25
  • A. Actually that code now looks a bit silly, just read it as $data['Charity'][], no need for $key. B. Doesn't the data need to be in the format $this->data['User'] = array(0=>array('etc'),1=>array('etc'))? Thanks – Will Mar 18 '11 at 10:28
  • You only add numeric indexes to the $data array only if it is for multiple records (see [examples of $data arrays](http://book.cakephp.org/view/1031/Saving-Your-Data)). But if you are saving only one record, the array must be like `$data['ModelName']['fieldname']`. So in your original code, you are doing a foreach, taking the iterated $charity which is a string and assigning it to `$data['Charity'][]`. This will cause the following: `$data['Charity'][0] => 'iterated_charity_value'`. See? No place whatsoever for fieldname or subarray (in case of saving multiple fields). – YOMorales Mar 18 '11 at 18:29