我正在为我的自行车马球联赛制作一些比赛计分软件。目标是在重复比赛开始前,每个人都有机会与其他球队比赛。
在我的Match Entity类中,我有一个名为createMatches的函数,它应该查找所有球队和相关的比赛。比赛和球队之间有一个HABTM关系,有一个连接表。这种关系运行良好—我在几个控制器方法中选择了团队(包含比赛)和比赛(包含球队),通过表单保存了关联,等等。但即便如此,在这个Entity函数中,我还是得到了错误"Teams is not associated with Matches"。这可能是由使用自动表引起的吗?...."等等。谁能告诉我怎么了?
这是讨论的方法。
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;
}
下面是Match.php中的init函数:
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'
]);
}
下面是Team.php中的init函数:
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'
]);
}
我想我没有碰过这两个函数——它们都是由cake bake生成的。
错误消息非常准确,但不是描述性的。它说Teams与Matches不关联。
嗯,我用最小的设置测试了这个,唯一的区别是,我没有把我的表放在插件里。没有检测到错误。所以,我很确定CakePHP不能找到你的表类在SwissRounds
-plugin中出于某种原因。
如果你的表是在一个SwissRounds插件,你应该使用插件符号的关联:
$this->belongsToMany('SwissRounds. s. '团队",[…和
$this->belongsToMany('SwissRounds. ')匹配",[…