CakePHP 3.0 cannot get 2 items from 1 table

旧时模样 提交于 2019-12-02 07:06:59

Problem is in definition of belongsTo associations. Try to redefine it this way:

$this->belongsTo('HomeClub', [
    'className' => 'Club',
    'foreignKey' => 'home_id',
    'propertyName' => 'home_club'
]);
$this->belongsTo('AwayClub', [
    'className' => 'Club',
    'foreignKey' => 'away_id',
    'propertyName' => 'away_club'
]);

Names of belongsTo associations have to be unique. Now contain them in the controller

// ...
$this->paginate = [
    'contain' => ['HomeClub', 'AwayClub']
];
$this->set('matches', $this->paginate($this->Match));

And then in the template use

<?= $match->home_club->name ?>
<?= $match->away_club->name ?>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!