doubiao9775 2013-07-26 07:13
浏览 11

模型HABTM模型参考原始模型

I have a Team model that is HABTM Match and when I pull the data for a specific Team, I ask for the related Teams for those Matches, but Cake only returns the data for the original Team.

How can I get all the Teams (the opponent) for that Match without looping through the results and pulling them that way?

I am having the same issue with the Team HABTM Player association as well. When I pull a Player, Cake will not return any of the associated Players (teammates) for the linked Team.

My schema:

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 ;

My query:

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

I have also tried:

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

The results ($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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 目详情-五一模拟赛详情页
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?
    • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
    • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
    • ¥15 cmd cl 0x000007b
    • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line