0
我有一个模型调用播放列表。 播放列表与跟踪HABTMCakePHP。找到返回多个HABTM关系
public $hasAndBelongsToMany = array(
'Track' => array(
'className' => 'Track',
'joinTable' => 'tracklists_tracks',
'foreignKey' => 'tracklist_id',
'associationForeignKey' => 'track_id',
'unique' => 'keepExisting',
'conditions' => '',
'fields' => array('id','title','version'),
'order' => 'TracklistsTrack.timing'
)
);
跟踪与APPUSER一个HABTM现在
public $hasAndBelongsToMany = array(
'Artist' => array(
'className' => 'AppUser',
'joinTable' => 'artists_tracks',
'foreignKey' => 'track_id',
'associationForeignKey' => 'artist_id',
'unique' => 'keepExisting',
'conditions' => '',
'fields' => array('id','username')
)
);
,我想,当我写这篇文章
$曲目列表= $这个 - > Tracklist- > findById($ ID);
为每个曲目获取所有曲目(并且这的确按照预期工作),并为每个曲目的所有AppUsers(级联)。
请注意,如果我写$ this-> Track-> findById($ id); 我实际上可以获得与该曲目相关的AppUsers
任何想法都可以通过我的勤奋命名约定来开箱即用吗?
谢谢
下能否请您写这两种模式?我与AppUser和Artist混淆。 – chetanspeed511987
建议:通常您应该将您的HABTMs转换为hasMany,就像加入模型一样,以获得更好的灵活性。 habtms变得非常难以管理,你可以阅读:http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasmany-through-the-join-model和http:// book.cakephp.org/2.0/en/models/saving-your-data.html#what-to-do-when-habtm-becomes-complicated –
当然,Artist是我用于AppUser的别名,因为曲目会有艺术家,但也有AppUsers的混音师。 – Snick