0

I would love to know the answer to this one as well - https://stackoverflow.com/questions/24447879/cakephp-habtm-i-want-duplicates-at-save

I've tried a few different things in regards to unique = false, saveAll saveAssocaited, array formatting. No luck.

My specific example is passing this array of data, and I'm using saveAll in my AvailablePackageController:

'AvailablePackage' => array(
    'title' => 'Tester',
    'description' => 'Tester description',
    'price' => '500.00',
    'image' => array(
        'name' => '',
        'type' => '',
        'tmp_name' => '',
        'error' => (int) 4,
        'size' => (int) 0
    )
),
'AvailableLesson' => array(
    'AvailableLesson' => array(
        (int) 0 => '23',
        (int) 1 => '23',
        (int) 2 => '23',
        (int) 3 => '9',
        (int) 4 => '9'
    )
)

As you can see I'd like this Available Package to have the Available Lesson '23' associated 3 times. As within a package you can take the same lesson 3 times over (in this case).

HABTM is setup correctly and all works as expected. In the above it saves 2 rows one for 23 and one for 9. So no issues in configuration of the relationship. It just won't save duplicates.

Any help would be greatly appreciated. Thanks.

Community
  • 1
  • 1

2 Answers2

0

That's a complicated HABTM relationship

And it's alluded to in the docs:

By default when saving a HasAndBelongsToMany relationship, CakePHP will delete all rows on the join table before saving new ones. For example if you have a Club that has 10 Children associated. You then update the Club with 2 children. The Club will only have 2 Children, not 12.

Don't use HABTM for that, make the join table a model

A habtm relationship is a simple:

Thing <-has and belongs to many-> OtherThing

If the number of links between two models has some significance then almost certainly it's not a simple HABTM relationship and is in fact something else e.g.

  • Competition hasMany Winner
  • Winner belongsTo Competition
  • Winner belongsTo User
  • Winner belongsTo Prize
  • (Winner has a property position)
  • User hasMany Prize

In this case, make the join table a model, treat it as a hasMany relationship and the problems of using HABTM to manipulate the data evaporate.

it's still possible to use HABTM relationships when desired and appropriate.

Community
  • 1
  • 1
AD7six
  • 63,116
  • 12
  • 91
  • 123
  • Thanks AD7six, I understand what your saying and have a model in place already that I use that for the admin side of things. I tested directly through the AvailableLessonsAvailablePackages model and the scenario I'm after can be added. What I'm having a hard time figuring out is how to use the HABTM Model directly from the AvailablePackages model. Do I set up as HABTM or HasMany? And what might the array structure need to look like after that. If ya have a link that'd be awesome! – Mattcoco35 Aug 27 '14 at 19:17
  • `Do I set up as HABTM or HasMany?` It would appear you haven't understood the answer =). I've reworded it to add further emphasis. – AD7six Aug 28 '14 at 13:55
  • Yahoooo! Worked like a charm. Had to alter array structure a little (for hasMany) but good to go. Thanks! data[AvailableLessonsAvailablePackages][][available_lesson_id] – Mattcoco35 Aug 28 '14 at 19:34
  • I believe that what the documentation says can't be used to justify the use of two "hasMany". But the "hasMany" alternative solves the problem indeed. – Gustavo Meira Aug 07 '15 at 17:55
0

A separate table named availablelessons_availablepackages is to be there and you need to define HABTM for the AvailableLesson and AvailablePackage models with foreignkey and associatedforeignkey and then you can save. All saved data will be in the availablelessons_availablepackages table.

FMQB
  • 653
  • 5
  • 12
  • FMQB, I played with this option as well. I think for this instance the HABATM work around makes sense. But your theory will work better once an AvailablePackage is booked through my system, turning it into a Package with Lesson associations. – Mattcoco35 Aug 27 '14 at 19:19