且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在cakephp 3中用两个外键链接同一张表

更新时间:2023-08-28 14:39:22

您没有正确设置关联

TeamsTable.php

$this->hasMany('MatchSchedulesA', [
    'foreignKey' => 'team_a',
    'className' => 'MatchSchedules'
]);
$this->hasMany('MatchSchedulesB', [
    'foreignKey' => 'team_b',
    'className' => 'MatchSchedules'
]);

在MatchSchedulesTable.php中

$this->belongsTo('TeamsA', [
    'foreignKey' => 'team_a',
    'joinType' => 'INNER',
    'className' => 'Teams'
]);
$this->belongsTo('TeamsB', [
    'foreignKey' => 'team_b',
    'joinType' => 'INNER',
    'className' => 'Teams'
]);

$matches = $this->MatchSchedules->find('all', [
  'contain' => [
      'TeamsA',
      'TeamsB
  ]
]);

很高兴您重新命名:

MatchSchedulesA to HomeMatches 
MatchSchedulesB to GuestMatches 
team_a to home_team 
team_b to guest_team 
TeamsA to HomeTeams 
TeamsB to GuestTeams