2

我正在尝试学习 CakePHP,但我很难获得一些数据。我正在开发一个跟踪曲棍球统计数据(真正的基本统计数据)的网站。我有 5 张桌子。

  • 季节(id、date_start、date_end)
  • 比赛(id、日期、score_team1、score_team2、team1_id、team2_id、season_id)
  • 团队(id,名称,颜色)
  • 玩家(id、first_name、last_name)
  • player_teams (id, player_id, team_id)

我能够从游戏视图中获得游戏的 2 支球队。我还能够从团队视图中获取团队的所有球员。

从比赛的角度来看,我需要帮助弄清楚如何让球队的所有球员都参与进来。我尝试了很多东西,但似乎都没有。我很难通过从我的游戏视图中执行 $game['Team1']['Players'] 之类的操作来获得一个团队的所有球员,但它不起作用。我试图从一场比赛中深入了解一支球队的球员。Season->Game->Team1->Players,但我被困在 team1 级别。

这是我的一些代码

/cakephp/teams -> 调试($teams[1])

array(
    'Team' => array(
        'id' => '2',
        'name' => 'Rouge',
        'color' => 'Rouge'
    ),
    'Players' => array(
        (int) 0 => array(
            'id' => '2',
            'first_name' => 'Player2FN',
            'last_name' => 'Player2LN',
            'PlayersTeam' => array(
                'id' => '2',
                'player_id' => '2',
                'team_id' => '2'
            )
        ),
        (int) 1 => array(
            'id' => '4',
            'first_name' => 'Player4FN',
            'last_name' => 'Player4LN',
            'PlayersTeam' => array(
                'id' => '4',
                'player_id' => '4',
                'team_id' => '2'
            )
        )
    )
)

但是从游戏详细信息视图中我得到了这个 -> debug($game) :

array(
    'Game' => array(
        'id' => '2',
        'date' => '2012-09-16 19:30:00',
        'score_team1' => '8',
        'score_team2' => '4',
        'team1_id' => '2',
        'team2_id' => '1',
        'season_id' => '1'
    ),
    'Team1' => array(
        'id' => '2',
        'name' => 'Rouge',
        'color' => 'Rouge'
    ),
    'Team2' => array(
        'id' => '1',
        'name' => 'Bleu',
        'color' => 'Bleu'
    ),
    'Season' => array(
        'id' => '1',
        'date_start' => '2012-09-09',
        'date_end' => '2013-04-07'
    )
)

我很确定这是一个模型问题,所以这是我的团队和游戏模型

<?php
    class Game extends AppModel {
        public $name = 'Game';
        public $belongsTo = array(
            'Season',
            'Team1' => array (
                'className' => 'Team',
                'foreignKey' => 'team1_id'
            ),
            'Team2' => array (
                'className' => 'Team',
                'foreignKey' => 'team2_id'
            )
        );
    }
?>

<?php
    class Team extends AppModel {
        public $name = 'Team';
        public $hasAndBelongsToMany = array(
            'Players' =>
                array(
                    'className'              => 'Player',
                    'joinTable'              => 'players_teams',
                    'foreignKey'             => 'team_id',
                    'associationForeignKey'  => 'id'
                )
        );
    }
?>
4

1 回答 1

1

采用

   $this->Game->recursive=2; 
   $this->Game->find('all');
于 2012-08-03T18:16:19.247 回答