I'm having an issue with Cake joining tables in the wrong place :(
I have three tables - CC_PLAYLIST
, CC_PLAYLISTCONTENTS
, and CC_FILES
.
I'd like to get a list of all playlists, with all of their contents, and then the relevant file information for each piece of playlist content.
I.E
Playlist 1
-- Playlist Content 1
---- File Info
-- Playlist Content 2
---- File Info
-- Playlist Content 3
---- File Info
Playlist 2
-- Playlist Content 123
---- File Info
In my Model (AirtimePlaylist) I get this data with
$result = $this->find('all', array(
'contain' => array(
'AirtimePlaylistContent' => array(
'fields' => array('AirtimePlaylistContent.id', 'AirtimePlaylistContent.playlist_id', 'AirtimePlaylistContent.file_id'),
'AirtimeFile' => array(
'fields' => array('AirtimeFile.id', 'AirtimeFile.track_title', 'AirtimeFile.artist_name')
)
),
),
'conditions' => array('AirtimePlaylist.id' => 5),
'fields' => 'AirtimePlaylist.id', 'AirtimePlaylist.name'
));
It seems however that the join between CC_PLAYLISTCONTENTS
and CC_FILES
is not quite playing ball. CC_PLAYLISTCONTENTS
should join CC_FILES
with CC_PLAYLISTCONTENTS
.file_id
= CC_FILES
.id
but it seems to be doing CC_PLAYLISTCONTENTS
.id
= CC_FILES
.id
AirtimePlaylist.php
class AirtimePlaylist extends AppModel {
public $useTable = 'cc_playlist';
public $actsAs = array('Containable');
public $hasMany = array('AirtimePlaylistContent' => array(
'className' => 'AirtimePlaylistContent',
'foreignKey' => 'playlist_id'
));
AirtimePlaylistContent.php
class AirtimePlaylistContent extends AppModel {
public $useTable = 'cc_playlistcontents';
public $hasOne = array('AirtimePlaylist' => array(
'className' => 'AirtimePlaylist',
'foreignKey' => 'id'
),'AirtimeFile' => array(
'className' => 'AirtimeFile',
'foreignKey' => 'id'
));
AirtimeFile.php
class AirtimeFile extends AppModel {
public $useTable = 'cc_files';
public $hasMany = array('AirtimeFileAttribute' => array(
'className' => 'AirtimeFileAttribute',
'foreignKey' => 'track_id'
));