2014-08-30 28 views
0

我有一個有很多視頻的回合桌。我的視頻表與玩家之間有許多關係。在我的回合視圖中,我展示了相關的視頻,但我試圖在回合視圖中向視頻的相關玩家展示。但我在這方面失敗了。我發佈了可以工作的代碼,那就是我展示相關視頻的回合視圖。cakePHP - 多對多 - 訪問玩家並在回合視圖中顯示他們

如何在回合視圖中訪問與視頻相關的球員?這正在擾亂我一段時間!

發表:

round_id int(11)   
round_name varchar(45) 
tournament_id int(11) 

視頻表:

video_id int(11) 
video_title varchar(45) 
video_date date 
video_scoreA 
video_scoreB 
video_url varchar(255) 
tournament_id int(11) 
round_id int(11) 

玩家表:

player_id int(11) 
player_firstname varchar(45) 
player_surname varchar(45) 
player_birthDate date 
player_turnedPro year(4) 
player_nickname varchar(45) 
player_nationality varchar(45) 
player_flag varchar(255) 
player_highestBreak varchar(45) 
player_highestRanking int(11) 
player_centuryBreaks int(11)s 
player_careerWinnings varchar(55) 
player_worldChampion varchar(45) 
player_image varchar(255) 
player_category varchar(45) 

players_videos表:

id int(11) 
video_id int(11) 
player_id int(11) 

Roundscontroller觀點行動:

public function view($id = null) { 
    if (!$this->Round->exists($id)) { 
     throw new NotFoundException(__('Invalid round')); 
    } 
    $options = array('conditions' => array('Round.' . $this->Round->primaryKey => $id)); 
    $this->set('round', $this->Round->find('first', $options)); 
} 

大紅大紫view.ctp:

<table cellpadding = "0" cellspacing = "0"> 
    <tr> 
     <th><?php echo __('Video Id'); ?></th> 
     <th><?php echo __('Video Title'); ?></th> 
     <th><?php echo __('Video Date'); ?></th> 
     <th><?php echo __('Video ScoreA'); ?></th> 
     <th><?php echo __('Video ScoreB'); ?></th> 
     <th><?php echo __('Video Url'); ?></th> 
     <th><?php echo __('Tournament Id'); ?></th> 
     <th><?php echo __('Round Id'); ?></th> 
     <th class="actions"><?php echo __('Actions'); ?></th> 
    </tr> 
    <?php foreach ($round['Video'] as $video): ?> 
     <tr> 
      <td><?php echo $video['video_id']; ?></td> 
      <td><?php echo $video['video_title']; ?></td> 
      <td><?php echo $video['video_date']; ?></td> 
      <td><?php echo $video['video_scoreA']; ?></td> 
      <td><?php echo $video['video_scoreB']; ?></td> 
      <td><?php echo $video['video_url']; ?></td> 
      <td><?php echo $video['tournament_id']; ?></td> 
      <td><?php echo $video['round_id']; ?></td> 
     </tr> 
    <?php endforeach; ?> 
</table> 

回答

0

的ContainableBehavior是你想要什麼:http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html

所以去你的AppModel並添加public $actsAs = array('Containable');它。

在控制器方法中添加contain關鍵選項陣:

$options = array(
    'conditions' => array('Round.' . $this->Round->primaryKey => $id), 
    'contain' => array(
     'Video' => array('Player'), 
    ), 
) 

在你看來,你可以從第一視頻一樣訪問的第一個球員名:

​​

或與foreach循環:

foreach ($round['Video'] as $video){ 
    echo $video['video_title']; 
    foreach ($video['Player'] as $player){ 
     echo $player['player_surname']; 
    } 
} 
相關問題