我正在爲我的自行車馬球聯賽制作一些比賽得分軟件。我們的目標是以一種讓每個人都有機會在重複比賽開始前與其他球隊比賽的機會來配對球隊。即使HABTM關聯存在,爲什麼會出現「未關聯」錯誤?
在我的匹配實體類中,我有一個名爲createMatches的函數,它可以查找所有團隊和關聯的匹配項。比賽和球隊之間有一個HABTM關係,並有一個連接表。這種關係正常工作 - 我選擇了幾個控制器方法中的團隊(包含匹配項)和匹配項(包含團隊),通過表單保存了關聯項等等。但即使如此,在此實體功能中,我收到錯誤「團隊與匹配不相關,這可能是由於使用自動錶?」等等。誰能告訴我什麼是錯的?
下面是有問題的方法。
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初始化函數:
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初始化函數:
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'
]);
}
我不相信我感動任這些功能 - 它們都是由蛋糕烘烤產生的。
如果主要的問題涉及到協會,這是一個好主意,包括那臺協會的那些(一個或多個)代碼 – Dave
很公平 - 編輯。 – Kluny