0
votes

Im having some problems with saving my HABTM relationship for some code I'm working with. I have Podcasts and Categories in a HABTM relationship. Code and problem explanation below

Model/Podcast.php

class Podcast extends AppModel{
    public $hasMany = 'Episode';
    public $hasAndBelongsToMany = array(
        'Category' =>
            array(
                'className' => 'Category',
                'joinTable' => 'categories_podcasts',
                'foreignKey' => 'podcast_id',
                'associationForeignKey' => 'category_id',
                'unique' => false
            )
        );
}

Model/Category.php

class Category extends AppModel{
    public $hasAndBelongsToMany = array(
        'Podcast' =>
            array(
                'className' => 'Podcast',
                'joinTable' => 'categories_podcasts',
                'foreignKey' => 'category_id',
                'associationForeignKey' => 'podcast_id',
                'unique' => false
            )
        );
}

This is what the array I'm passing into saveAll() looks like

Array
(
    [Podcast] => Array
        (
            [name] => Mohr Stories - FakeMustache.com
            [description] => Join your host Jay Mohr and a rotating cast of his hilarious friends as they regale you with tales and conversation covering every topic imaginable. You haven't heard the half of it until you've heard... Mohr Stories.
            [website] => http://www.FakeMustache.com
        )

    [Category] => Array
    (
        [0] => 1
        [1] => 2
    )

)

Which works fine - the podcast is added and the join table is updated, but I was under the impression that my Category array could look like the following:

 [Category] => Array
        (
            [0] => Array
                (
                    [name] => Test
                )

            [1] => Array
                (
                    [name] => Test2
                )

        )

Which would save the new categories "Test" and "Test2" and then update the join table with their newly added ids. Is this not possible, or I am missing some of the picture here?

1
I was under the impression - based on what documentation?AD7six
Based on this section of the Saving Your Data page of the docs. Their example array there and the description "Passing the above array to saveAll() will create the contained tags, each associated with their respective recipes."Bizarro181

1 Answers

0
votes

You do are able to do that, but with another data format in which each record contains the associated data as on the following code that allows to save 2 records :

array{
    array{
        'Podcast' => array {
             'id' => 25
         },
         'Category' => array {
             'name' => 'Test'
         }
    }
    array{
         'Podcast' => array {
             'id' => 25
         },
         'Category' => array {
             'name' => 'Test2'
         }
    }
}

Pass this array to the saveAll method on one of your Podcast/Category Models.

$this->Podcast->saveAll($myData);

But take care, as you don't provide any id for the categories, they will be created, even if there are existing Category with the same name.

And if a Podcast with the id 25 doesn't exists, it will be created.