模型HABTM模型参考原始模型

时间:2013-07-26 07:13:49

标签: php cakephp has-and-belongs-to-many

我有一个HABTM Match的团队模型,当我为特定团队提取数据时,我会要求相关团队参与这些匹配,但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 ...

0 个答案:

没有答案