0
votes

I know there is a lot of questions with this specific problem and there are examples in the official documentation. But I could not find out what I am doing wrong.

I have two Model with a HABTM relation, but apparently it is not saving anything in the join and final table. These are my tables, models and array I am trying to save.

Tables:

champions table:

CREATE TABLE `champions` (
 `id` int(11) unsigned NOT NULL,
 `key` varchar(50) DEFAULT NULL,
 .
 .
 .
 `created` datetime NOT NULL,
 `modified` datetime NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

champions_tags table:

CREATE TABLE `champions_tags` (
 `id` int(11) NOT NULL,
 `champion_id` int(11) NOT NULL,
 `tag_id` int(11) NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

tags table:

CREATE TABLE `tags` (
 `id` int(11) NOT NULL,
 `type` varchar(50) DEFAULT NULL,
 `created` datetime NOT NULL,
 `modified` datetime NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

Models:

Champion.php

public $hasAndBelongsToMany = array(
    'Tag'   =>  array(
                'foreignKey'            =>  'champion_id',
                'associationForeignKey' =>  'tag_id',
                'unique'                =>  'keepExisting'
     )
);

Tag.php

    public $hasAndBelongsToMany = array(
        'Tag'  =>  array(
            'foreignKey'            =>  'tag_id',
            'associationForeignKey' =>  'champion_id',
            'unique'                =>  'keepExisting'
        )
    );

Finally this is the info I am trying to save and spread to tags table

    Array
    (
        [Champion] => Array
            (
                .
                .
                .
                [Tag] => Array
                    (
                        [0] => Array
                            (
                                [id] => 2660
                                [type] => Fighter
                            )

                        [1] => Array
                            (
                                [id] => 2661
                                [type] => Tank
                            )

                    )

            )
    )
1

1 Answers

0
votes

In CakePHP 2.x, HABTM is tricky. It's never easy to get how it work, especially when it comes to saving the data.

For your case, your data should look like this :

<?php
$this->Champion->save([
    'Champion' => [
        'key' => 'championKey',
    ],
    'Tag' => [
        'Tag' => [2660, 2661],
    ],
]);

What if you want to associate a tag that don't exists yet? You have to created it first, then add its id to the $data['Tag']['Tag'] list.

You can in the same time save a champion and create a new tag, but unfortunately it has to be done one by one :

<?php
$this->Champion->save([
    'Champion' => [
        'id' => 1,
    ],
    'Tag' => [
        'type' => 'new type 1',
    ]
]);
$this->Champion->save([
    'Champion' => [
        'id' => 2,
    ],
    'Tag' => [
        'type' => 'new type 2',
    ]
]);
$this->Champion->save([
    'Champion' => [
        'id' => 2,
    ],
    'Tag' => [
        'type' => 'new type 3',
    ]
]); // /!\ Champion #2 is not tagged with 'new type 2' anymore!

You can find all those informations in the docs :)