CakePHP 中的多个 belongsTo() 关联 3.x

Multiple belongsTo() associations in CakePHP 3.x

我正在使用 CakePHP 3.x。
我的问题是:是否可以有多个 belongsTo() 关联与相同的外键?

这是我的问题:
我的 table 中有三个字段使用相同的外键。

在模型 Table 中,我这样使用 belongsTo() 关联:

$this->belongsTo('Pilotes', [
    'className' => 'Users',
    'foreignKey' => 'pilote',
    'propertyName' => 'pilote_user'
]);

$this->belongsTo('Verificateurs', [
    'className' => 'Users',
    'foreignKey' => 'no_user',
    'propertyName' => 'verificateur_user'
]);

$this->belongsTo('Users', [
    'className' => 'Users',
    'foreignKey' => 'no_user',
    'propertyName' => 'user'
]);

但是当我调试我的实体时,前两个字段只包含外键,最后一个没问题。

查询:

public function view($id = null)
{
    $demande = $this->Demandes->get($id, [
        'contain' => ['Users']
    ]);
    $this->set('demande', $demande);
    $this->set('_serialize', ['demande']);
}

这里是 debug() 的输出:

object(App\Model\Entity\Demande) {
'new' => false,
'accessible' => [
    'pilote' => true,
    'verificateur' => true,
    'pilote_user' => true,
    'verificateur_user' => true,
    'user' => true,
],
'properties' => [
    'no_demande' => (int) 4,
    'pilote' => (int) 3,
    'verificateur' => (int) 2,
    'no_user' => (int) 1,
    'user' => object(App\Model\Entity\User) {
        /* Details of the user */
    },
],
'dirty' => [],
'original' => [],
'virtual' => [],
'errors' => [],
'repository' => 'Demandes'
}

如果我评论最后一个 belongsTo(),第二个字段可以,但第一个字段不行。

提前致谢

更改第一个参数中的名称:

$this->belongsTo('Pilotes', [
    'className' => 'Users',
    'foreignKey' => 'pilote',
    'propertyName' => 'pilote_user'
]);

$this->belongsTo('Verificateurs', [
    'className' => 'Users',
    'foreignKey' => 'verificateur',
    'propertyName' => 'verificateur_user'
]);

$this->belongsTo('Users', [
    'className' => 'Users',
    'foreignKey' => 'no_user',
    'propertyName' => 'user'
]);

请注意,当您选择使用 piloteverificateur 作为架构中的列名称时,您需要更改 propertyName 的值。这是因为 Cake 将在您的实体中创建一个具有该名称的 属性,它会与您的 table 中的属性发生冲突。

例如,这就是我必须选择 pilote_user 作为您实体的 属性 名称的原因。

查询数据时,您需要将 'contain' => ['Users', 'Verificateurs', 'Pilotes'] 添加到查找器选项中