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 ...