0

This seems so simple but im banging my head against the wall.

I have a simple HABTM relationship (Artists hasMany Artists) and im trying to save some data.

I have two tables: artists: (id, name, created, modified) artists_related: (id, artist_id, related_id)

Heres what my code looks like, models/artist.php:

class Artist extends AppModel {
var $name = 'Artist';
var $hasAndBelongsToMany = array(        
    'Related' => array(            
        'className'     => 'Artist',
        'joinTable'     => 'artists_related',         
        'foreignKey'    => 'artist_id',
        'associationForeignKey' => 'related_id'
    )   
);  

function saveArtist($slug) {

    $data = array(
        'Artist' => array(
            'name' => 'Alicia Keys'
        ),
        'Related' => array(
            array(
                'name' => 'Mariah Carey'
            ),
            array(
                'name' => 'Beyoncé'
            )
        )
    );
$result = $this->saveAll($data);

The results of the saveAll() is that 1 row (Alicia Keys) gets inserted into the artists table. The related artists do not, and nothing gets inserted into the join table.

What am i doing wrong?

tereško
  • 58,060
  • 25
  • 98
  • 150
lasse
  • 25
  • 5

1 Answers1

0

I did't get what you are trying to do , once go through this link this may help

http://book.cakephp.org/1.3/en/view/1034/Saving-Related-Model-Data-HABTM

Anil kumar
  • 4,107
  • 1
  • 21
  • 36
  • Thanks for the answer. I am trying to save an artist and her related artists in the **artists** table. And the relation in the **artists_related** table. It's different from the example in the page because its not between 2 different models, its a habtm to the same model. – lasse Oct 31 '12 at 08:22
  • As in cookbook you can select other artists through a select dropdown ,$this->set('artists', $this->Artist->find('list')); and in view $this->Form->input('artists'); – Anil kumar Oct 31 '12 at 09:07