CakePHP 3 - Assocation BelongsToMany 和 hasMany - 将列限制为 select
CakePHP 3 - Assocation BelongsToMany and hasMany - limit columns by select
美好的一天!
我在使用 CakePHP 3 时遇到了一些小问题。我有这样的关联:
$this->belongsToMany('AdminUserGroup',[
'classname' => 'AdminUserGroup',
'foreignKey' => 'admin_user_id',
'targetForeignKey' => 'group_id',
'joinTable' => 'AdminUsersGroups'
]);
我正在使用此代码返回记录:
public function getAll()
{
$ble = $this->find()
->contain(['AdminUserGroup' ]);
return $ble;
}
直到它起作用,但是当我想 select 指定的字段时我遇到了问题。当我添加 select 方法时,我看不到包含 table:
的列
public function getAll()
{
$ble = $this->find()->select(['id', 'name', 'surname'])
->contain(['AdminUserGroup']);
return $ble;
}
所以我添加了回调查询:
public function getAll()
{
$ble = $this->find()->select(['id, name, surname'])
->contain(['AdminUserGroup' => function ($q) { return $q->select(['group_name']);}]);
return $ble;
}
但是还是不行。我只能看到 main table 中的字段。包含 table 的字段不会出现。
{
"id": "8",
"name": "Ola",
"lastname": "BleBle",
"admin_user_group": []
},
我可以修吗?
尝试这样的事情。
$ble = $this->find()->select(['Table.id, Table.name, Table.surname'])->contain(['AdminUserGroup']);
manual 包括以下评论:
When you limit the fields that are fetched from an association, you must ensure that the foreign key columns are selected. Failing to select foreign key fields will cause associated data to not be present in the final result.
和
If you have limited the fields you are loading with select() but also want to load fields off of contained associations, you can pass the association object to select().
和
Alternatively, if you have multiple associations, you can use autoFields().
那里有有用的示例。所以:
$ble = $this->find()
->select(['id', 'name', 'surname'])
->contain(['AdminUserGroup' ])
->autoFields(true);
或者如果您使用的是 3.1 或更高版本:
$ble = $this->find()
->select(['id', 'name', 'surname'])
->select($this->AdminUserGroup);
从示例来看,第二个版本可能不需要 contain
调用,因此我将其省略。
美好的一天! 我在使用 CakePHP 3 时遇到了一些小问题。我有这样的关联:
$this->belongsToMany('AdminUserGroup',[
'classname' => 'AdminUserGroup',
'foreignKey' => 'admin_user_id',
'targetForeignKey' => 'group_id',
'joinTable' => 'AdminUsersGroups'
]);
我正在使用此代码返回记录:
public function getAll()
{
$ble = $this->find()
->contain(['AdminUserGroup' ]);
return $ble;
}
直到它起作用,但是当我想 select 指定的字段时我遇到了问题。当我添加 select 方法时,我看不到包含 table:
的列public function getAll()
{
$ble = $this->find()->select(['id', 'name', 'surname'])
->contain(['AdminUserGroup']);
return $ble;
}
所以我添加了回调查询:
public function getAll()
{
$ble = $this->find()->select(['id, name, surname'])
->contain(['AdminUserGroup' => function ($q) { return $q->select(['group_name']);}]);
return $ble;
}
但是还是不行。我只能看到 main table 中的字段。包含 table 的字段不会出现。
{
"id": "8",
"name": "Ola",
"lastname": "BleBle",
"admin_user_group": []
},
我可以修吗?
尝试这样的事情。
$ble = $this->find()->select(['Table.id, Table.name, Table.surname'])->contain(['AdminUserGroup']);
manual 包括以下评论:
When you limit the fields that are fetched from an association, you must ensure that the foreign key columns are selected. Failing to select foreign key fields will cause associated data to not be present in the final result.
和
If you have limited the fields you are loading with select() but also want to load fields off of contained associations, you can pass the association object to select().
和
Alternatively, if you have multiple associations, you can use autoFields().
那里有有用的示例。所以:
$ble = $this->find()
->select(['id', 'name', 'surname'])
->contain(['AdminUserGroup' ])
->autoFields(true);
或者如果您使用的是 3.1 或更高版本:
$ble = $this->find()
->select(['id', 'name', 'surname'])
->select($this->AdminUserGroup);
从示例来看,第二个版本可能不需要 contain
调用,因此我将其省略。