0
我有一個版本的視頻表。一個版本可以有很多視頻。cakePHP多對多的關係 - 加載表視圖內的視圖,與關係無關
視頻有很多玩家,玩家有許多視頻。所以視頻和玩家之間有多對多的關係。
在我的版本視圖中,我加載了與該版本相關的所有視頻。但我也想加載屬於視頻的球員信息。在數據庫中,版本和玩家之間沒有關係,因爲這種關係是在視頻和玩家之間(多對多 - 玩家視頻)。
我可以加載與視頻相關的players_id。我從player_videos表中得到這些id。
如何在版本視圖中加載屬於視頻的玩家信息?
版車型有很多關係:
public $hasMany = array(
'Video' => array(
'className' => 'Video',
'foreignKey' => 'edition_id',
'dependent' => false,
)
);
EditionsController觀點行動:
public function view($id = null) {
if (!$this->Edition->exists($id)) {
throw new NotFoundException(__('Invalid edition'));
}
$options = array('conditions' => array('Edition.' . $this->Edition->primaryKey => $id));
$this->set('edition', $this->Edition->find('first', $options));
$tab = array('Video.' . $this->Video->primaryKey => $id); // which you loop for the video list and fill with their ids
$playersvideos = $this->Edition->Video->PlayersVideo->find('all', array('conditions' => array('PlayersVideo.video_id' => $tab),'recursive'=>2));
$this->set('playersvideos ', $playersvideos);
}
view.ctp:
<?php foreach ($edition['Video'] as $video): ?>
<tr>
<td><?php echo $video['video_id']; ?></td>
<td><?php echo $video['video_title']; ?></td>
<td>...</td>
<td>
<?php foreach ($playersvideos as $playervideo): ?>
<?php echo $playervideo['PlayersVideo']['player_id']; ?>
<?php echo $playervideo['PlayersVideo']['video_id']; ?>
<?php endforeach; ?>
</td>
</tr>
<?php endforeach; ?>
玩家擁有屬於一對多的關係:
public $hasAndBelongsToMany = array(
'Video' => array(
'className' => 'Video',
'joinTable' => 'players_videos',
'foreignKey' => 'player_id',
'associationForeignKey' => 'video_id',
'unique' => 'keepExisting',
)
);
視頻擁有屬於一對多的關係:
public $hasAndBelongsToMany = array(
'Player' => array(
'className' => 'Player',
'joinTable' => 'players_videos',
'foreignKey' => 'video_id',
'associationForeignKey' => 'player_id',
'unique' => 'keepExisting',
)
);
播放器視頻屬於關係
public $belongsTo = array(
'Video' => array(
'className' => 'Video',
'foreignKey' => 'video_id',
),
'Player' => array(
'className' => 'Player',
'foreignKey' => 'player_id',
)
);
其中行是錯誤? – 2014-08-29 15:23:39
plz從查詢中刪除IN:$ players = $ this-> Edition-> Video-> Player-> find('all',array( 'conditions'=> array('Player.video_id'=> $ tab ), '遞歸'=> 2)); – 2014-08-29 15:27:59
等一會兒...... – 2014-08-29 15:31:12