I have an artist that has and belongs to many related artists, in this example i want to save Madonna and her 2 related artists Cher and Kylie Minogue in the database.
DB: 2 tables: artists (id, name, created) and artists_related (id, artist_id, related_id)
Model relationship setup:
<?php
class Artist extends AppModel {
public $hasAndBelongsToMany = array(
'Related' =>
array(
'className' => 'Artist',
'joinTable' => 'artists_related',
'associationForeignKey' => 'related_id',
'unique' => 'keepExisting'
)
);
I'm trying to save the data like this:
$test_data = array(
'Artist' => array(
'name' => 'Madonna'
),
'Related' => array(
0 => array(
'name' => 'Kylie Minogue'
),
1 => array(
'name' => 'Cher'
)
)
);
$result = $this->saveAll($test_data, array('deep' => true));
What happens is that only Madonna is saved in the artists table, the two related artists arent saved, and nothing is saved in the join table.
How should the data array be formatted to accomplish this? Or do i need to save first Madonna, and get the id and then save all the related artists in a different saveAll() ?