0

我有一个 HABTM 匹配的团队模型,当我提取特定团队的数据时,我会询问这些匹配的相关团队,但 Cake 只返回原始团队的数据。

我怎样才能在不循环结果并以这种方式拉动他们的情况下获得该比赛的所有球队(对手)?

我对 Team HABTM Player 协会也有同样的问题。当我拉出一名球员时,Cake 不会返回任何关联球队的相关球员(队友)。

我的架构:

CREATE TABLE IF NOT EXISTS `matches` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `tournament_id` int(10) unsigned NOT NULL,
  `name` varchar(255) NOT NULL DEFAULT '',
  `created` datetime NOT NULL,
  PRIMARY KEY (`id`),
  KEY `tournament_id` (`tournament_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 ;


CREATE TABLE IF NOT EXISTS `matches_teams` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `match_id` int(10) unsigned NOT NULL,
  `team_id` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `match_id` (`match_id`,`team_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 ;


CREATE TABLE IF NOT EXISTS `players` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 ;


CREATE TABLE IF NOT EXISTS `players_teams` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `player_id` int(10) unsigned NOT NULL,
  `team_id` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `player_id` (`player_id`,`team_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 ;


CREATE TABLE IF NOT EXISTS `teams` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `tournament_id` int(10) unsigned NOT NULL,
  `name` varchar(255) NOT NULL,
  `seed` smallint(2) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `tournament_id` (`tournament_id`),
  KEY `seed` (`seed`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 ;

我的查询:

$this->Team->recursive = 3;
$team = $this->Team->read(null, $id);
$this->set('team', $team);

我也试过:

$this->Team->contain(array(
    'Match' => array(
        'Team',
    ),
));
$team = $this->Team->read(null, $id);
$this->set('team', $team);

结果($team):

array (size=4)
  'Team' => 
    array (size=5)
      ... team data ...

  'Match' => 
    array (size=2)
      0 => 
        array (size=9)
          ... match data ...

          'MatchesTeam' => 
            array (size=3)
              'id' => string '1' (length=1)
              'match_id' => string '1' (length=1)
              'team_id' => string '1' (length=1)

        // there should be an array for 'Team' here
        // that contains the opponent team

      1 => 
        ... more match data with same missing 'Team' array ...

    ... other related models ...
4

0 回答 0