0

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 ...
Benjam
  • 5,285
  • 3
  • 26
  • 36
  • Can you add some information about your Models & Database setup please? – pudelhund Jul 26 '13 at 08:12
  • Also, using find() instead of read() probably is the cleaner way to do things. – pudelhund Jul 26 '13 at 08:13
  • @pudelhund is right try using $this->Team->findAllById($id); instead of your read statement – Scrappy Cocco Jul 26 '13 at 09:56
  • btw recursive = 3 does not exist in CakePHP. By default you should always use it at -1 (set it in AppModel.php) and use Contain (like you do). – Chris Jul 26 '13 at 13:16
  • @Chris you are right. `recursive` should be a value between -1 and 2, but I see no reason why does it **always** have to be at -1. – Cris Sawfish Jul 26 '13 at 13:45
  • @Benjam you must define the HABTM relation in both of your models. Also set `recursive` value to 2 in the model from which you are quering and you should have all your data fectched as you want them. – Cris Sawfish Jul 26 '13 at 13:48
  • @CrisSawfish- I do have HABTM defined in both. @Chris- From what I understand, there is no limit on `recursive`, and in my tests, it does actually pull more data when it's set higher. Regardless, the main issue remains. – Benjam Jul 26 '13 at 13:57
  • To clear things out. Check the [cakebook](http://book.cakephp.org/2.0/en/models/model-attributes.html#recursive) for valid values of `recursive`. It's crucial to set it correctly otherwise it will automatically be set to 1, which is the default. Also try that in both of your models, ie try the following `$this->Team->recursive = 2;` and `$this->Team->Match->recursive = 2;` – Cris Sawfish Jul 26 '13 at 14:03
  • @CrisSawfish- Set `recursive = 2` with no effect. Recursive isn't the issue here, because I get the same incorrect results when using `Contain`. – Benjam Jul 26 '13 at 14:07
  • @pudelhund- Schema information added as requested. – Benjam Jul 26 '13 at 14:08

0 Answers0