Monday, July 29, 2013

Model HABTM Model Referencing Original Model

This is a duplicate of my question asked on StackOverflow here: http://stackoverflow.com/questions/17875143/model-habtm-model-referencing-original-model
If you could answer the question there, or let me know if it is a bug or not, it would be greatly appreciated.

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

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments: