0
votes

I have 3 tables structure as...

SONG(id, status)

TRACKLIST(id, song_id, artist_id, status)

ARTIST(id, status)

I wish to have HABTM so I mentioned as.. Song MODEL

var $hasAndBelongsToMany = array(
    'Artist' => array(
        'className' => 'Artist',
        'joinTable' => 'tracklists',
        'foreignKey' => 'song_id',
        'associationForeignKey' => 'artist_id',
        'conditions' => array('tracklist.status' => '1')
    'with' => 'Tracklist',
    //'unique' => true
    ),
);

Artist Model

 var $hasAndBelongsToMany = array(
    'Song' => array(
        'className' => 'Song',
        'joinTable' => 'tracklists',
        'foreignKey' => 'artist_id',
        'associationForeignKey' => 'song_id',
        'with' => 'Tracklist',
        //'unique' => true
    ),
);

Tracklist Model var $belongsTo = array(

    'Song' => array(
        'className' => 'Song',
        'foreignKey' => 'song_id',
        'dependent' => true
    ),
    'Artist' => array(
        'className' => 'Artist',
        'foreignKey' => 'artist_id',
        'dependent' => true
    )
);

This way but issue is when I tried to find records from the Artist table it find all the records without any condition.

    $artist_conditions = array('Artist.status' => '1');
    $artist_list = $this->Song->Artist->find('list', array('conditions' => $artist_conditions ));

I wish to fetch only those artist related to the tracklist and having tracklist.status as 1. Is the relationship is correct?? Or I simply use hasMany with (Song, Artist) and BelongsTo with (Tracklist).

1

1 Answers

0
votes

You can try this:

/* for example in songs controller */
    public $uses = array('Song','Tracklist');
    $artist_conditions = array('Artist.status' => '1');
    $this->Tracklist->recursive = 1;
    $artist_list = $this->Tracklist->find('list', array('conditions' => $artist_conditions ));