【发布时间】:2015-05-07 21:14:38
【问题描述】:
我正在使用 CakePHP 3.x。
我的问题是:是否有可能有许多属于同一个外键的关联()?
这是我的问题:
我的表中有三个字段使用相同的外键。
在模型表中,我以这种方式使用了 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(),第二个字段很好,但不是第一个。
提前致谢
【问题讨论】:
-
什么查询会产生这个结果?
-
我在帖子中添加了查询
-
你需要包含其他关联
'contain' => ['Users', 'Verificateurs', 'Pilotes']
标签: cakephp cakephp-3.0