0

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).

tereško
  • 58,060
  • 25
  • 98
  • 150
Tisha
  • 13
  • 4

1 Answers1

0

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 ));
Arash Mousavi
  • 2,110
  • 4
  • 25
  • 47
  • You are not clear with my request of question. Please read it.. Our code is almost similar you are just adding recursive to 1. That's not solution. – Tisha Aug 25 '12 at 20:44
  • If you pay enough attention you can see , the solution is calling find function on Tracklist model, not Artist model! they are very different, please try it, maybe helps you. – Arash Mousavi Aug 25 '12 at 21:02