简体   繁体   中英

CakePHP (1.3) HABTM on the same model saveAll() not working

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM