CakePHP 3.0 cannot get 2 items from 1 table

前端 未结 1 1587
囚心锁ツ
囚心锁ツ 2021-01-23 08:51

In a football match i have 2 clubs \"Home-Club\" and \"Away-Club\" . I have created table \"match\" and \"club\" in MySQL. In \"match\" table has 2 foreign key \"home_id\" and \

相关标签:
1条回答
  • 2021-01-23 09:41

    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 ?>
    
    0 讨论(0)
提交回复
热议问题