I have got two Models in CakePHP 2.2.1:
class Episode extends AppModel {
public $hasAndBelongsToMany = array(
'Tags' => array('className' => 'Tag')
);
}
and
class Tag extends AppModel {
public $hasAndBelongsToMany = array(
'Episode' => array('className' => 'Episode')
);
}
And I want to insert an Episode and a Tag at the same time and link them to each other. From the documentation I got that this would be the right code which I execute inside a controller method:
$this->Tag->create();
$this->Tag->save(array(
'name' => 'Test Tag'
));
$tagid = $this->Tag->getInsertID();
var_dump($tagid); // works
$this->Episode->create();
$this->Episode->save(array(
'Episode' => array(
'title' => 'Test with Tag'
),
'Tags' => array(
'Tag' => array($tagid)
)
));
I get no error message and both records are created, but there is no row inserted into the episodes_tags table. CakePHP does these queries:
Nr Query Err Aff Rws Took (ms)
1 INSERT INTO `pottcast`.`tags` (`name`) VALUES ('Test Tag') 1 1 0
2 INSERT INTO `pottcast`.`episodes` (`title`) VALUES ('Test with Tag') 1 1 0
3 SELECT `EpisodesTag`.`tag_id` FROM `pottcast`.`episodes_tags` AS `EpisodesTag` WHERE `EpisodesTag`.`episode_id` = 9 0 0 0
What did I do wrong?