Ich denke, das ist eine einfache goesToMany Situation, mit der Ausnahme, dass der Name der Beziehung nicht die verbundenen Namen der Entitätstabellen ist. Dies liegt daran, dass die Beziehung sehr spezifisch ist. In der Join-Tabelle werden keine zusätzlichen Daten gespeichert, sodass die Option through nicht erforderlich ist.CakePHP3 gehörtToMany Abfrage wird nicht korrekt generiert
Instanzen:
table candidates {
id int,
<other fields>
}
table companies {
id int,
<other fields>
}
Tabelle Join (ermöglicht es dem Kandidaten ein Unternehmen zur Blacklist):
table blacklists {
id int,
candidate_id int,
company_id int
}
CandidatesTable.php:
$this->belongsToMany('BlacklistedCompanies', [
'className' => 'Companies',
'joinTable' => 'blacklists',
'foreignKey' => 'candidate_id',
'targetForeignKey' => 'company_id'
]);
Abfrage in der Steuerung:
$bl = $this->Candidates->find("all")->where(["id" => 1])->contain("BlacklistedCompanies")->all();
Sobald er ausgeführt wird, wird die folgende schlechte Abfrage generiert und wirft einen Fehler (1054 Unknown Column) - siehe Linie mit Pfeil unten:
SELECT
Blacklists.company_id AS `Blacklists__company_id`,
Blacklists.id AS `Blacklists__id`,
Blacklists.candidate_id AS `Blacklists__candidate_id`,
BlacklistedCompanies.id AS `BlacklistedCompanies__id`,
<remaining companies fields expunged>
FROM companies BlacklistedCompanies
LEFT JOIN blacklists Blacklists ON
(BlacklistedCompanies.id = (Blacklists.company_id)
AND Blacklists.id = (Blacklists.blacklist_id)) <<-- ERROR!!
WHERE Blacklists.candidate_id in (:c0)
Warum ist CakePHP für blacklist_id suchen ??? Es existiert nirgendwo. Blacklist ist eine Join-Tabelle, keine Entität mit Fremdschlüsseln, die auf sie verweisen !!
Was mache ich falsch?