0

I'm making some tournament scoring software for my bike polo league. The goal is to match up teams in such a way that everyone gets a chance to play every other team before repeat matches start.

In my Match Entity class, I have a function called createMatches that should find all teams and associated matches. There is a HABTM relationship between matches and teams, with a join table. This relationship works fine - I've selected teams (contain matches) and matches (contain teams) in several controller methods, saved associations through forms, and so on. But even so, in this Entity function, I get the error "Teams is not associated with Matches. Could this be caused by using Auto-Tables? ...." and so on. Can anyone tell me what's wrong?

Here's the method in question.

public function createMatches($tournamentId) {
    $team = TableRegistry::get('Teams', ['contain' => ['Matches']]);
        $teams = $team->find('all', ['conditions' =>
                ['tournament_id' => $tournamentId],
                    'contain' =>
            ['Matches' =>
                ['fields' =>
                    ['Matches.id']
             ]]]);
    return $teams;
}

Here's the init function from Match.php:

public function initialize(array $config)
{
    parent::initialize($config);

    $this->table('matches');
    $this->displayField('id');
    $this->primaryKey('id');

    $this->addBehavior('Timestamp');

    $this->belongsTo('Rounds', [
        'foreignKey' => 'round_id',
        'joinType' => 'INNER',
        'className' => 'SwissRounds.Rounds'
    ]);
    $this->belongsToMany('Teams', [
        'foreignKey' => 'match_id',
        'targetForeignKey' => 'team_id',
        'joinTable' => 'matches_teams',
        'className' => 'SwissRounds.Teams'
    ]);
}

Here's the init function from Team.php:

public function initialize(array $config)
{
    parent::initialize($config);

    $this->table('teams');
    $this->displayField('name');
    $this->primaryKey('id');

    $this->hasMany('Players', [
        'foreignKey' => 'team_id',
        'className' => 'SwissRounds.Players'
    ]);
    $this->belongsToMany('Matches', [
        'foreignKey' => 'team_id',
        'targetForeignKey' => 'match_id',
        'joinTable' => 'matches_teams',
        'className' => 'SwissRounds.Matches'
    ]);
}

I don't believe I've touched either of those functions - they were both generated by cake bake.

Kluny
  • 189
  • 3
  • 17

2 Answers2

0

Error message is quite exact, but not descriptive. It says that Teams is not associated with Matches.

Well, I tested this with minimal setup, with only difference, that I did not have my tables inside plugins. And no errors detected. So, I am pretty sure that CakePHP cant find your table classes inside SwissRounds -plugin for some reason.

makallio85
  • 1,366
  • 2
  • 14
  • 29
0

If your tables are in a SwissRounds plugin, you should use plugin notation for the associations:
$this->belongsToMany('SwissRounds.Teams', [... and
$this->belongsToMany('SwissRounds.Matches', [...

Tolga
  • 262
  • 5
  • 16