I'm getting a "Not Associated" error even though the association is in place

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;
}

Why this ‘contain’ in TableRegistry::get ?

Because I need the matches contained in the query. Is there some reason not to do that?

I am not sure but I would try to run the query first with find() and then apply contain on it like here:

// In a controller or table method.
$articles = TableRegistry::get('Articles');
$query = $articles->find()->contain(['Tags']);

http://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html

if it’s not the solution maybe try it with loadModels?

Hi kluny,

could you please let us know which CakePHP version are you using?

Thanks, I’ll try this next.

cool let me know if it worked out for you