从 Yii2 中的连接表中检索数据
我正在尝试从 Yii2 的连接表中获取数据,而无需额外查询.我有 2 个模型(用户、组)通过连接表 (user_group) 关联.在 user_group 表中,我想为此关系存储额外的数据(管理员标志,...).
I'm trying to get data from a join table in Yii2 without an additional query. I have 2 models (User, Group) associated via the junction table (user_group). In the user_group table, I want to store extra data (admin flag, ...) for this relation.
将数据添加到联结表的最佳方法是什么? link 方法接受一个参数 extraColumns 但我不知道它是如何工作的.
What's the best way to add data to the junction table? The link method accepts a parameter extraColumns but I can't figure out how this works.
检索这些数据的最佳方法是什么?我编写了一个额外的查询来从联结表中获取值.必须有更简洁的方法来做到这一点?!
What's the best way to retrieve this data? I wrote an additional query to get the values out of the junction table. There must be a cleaner way to do this?!
仅供参考,这就是我在模型中定义关系的方式:
FYI, this is how I defined the relation in the models:
Group.php
public function getUsers() {
return $this->hasMany(User::className(), ['id' => 'user_id'])
->viaTable('user_group', ['group_id' => 'id']);
}
用户.php
public function getGroups() {
return $this->hasMany(Group::className(), ['id' => 'group_id'])
->viaTable('user_group', ['user_id' => 'id']);
}
推荐答案
简而言之:像您建议的那样为连接表使用 ActiveRecord
恕我直言是正确的方法,因为您可以设置 via()
以使用现有的 ActiveRecord
.这允许您使用 Yii 的 link()
方法在连接表中创建项目,同时添加数据(如您的管理员标志).
In short: Using an ActiveRecord
for the junction table like you suggested is IMHO the right way because you can set up via()
to use that existing ActiveRecord
. This allows you to use Yii's link()
method to create items in the junction table while adding data (like your admin flag) at the same time.
官方 Yii 指南 2.0 声明了两种使用联结表的方法:使用 viaTable()
和使用 via()
(参见 此处).前者需要连接表的名称作为参数,后者需要一个关系名称作为参数.
The official Yii Guide 2.0 states two ways of using a junction table: using viaTable()
and using via()
(see here). While the former expects the name of the junction table as parameter the latter expects a relation name as parameter.
如果您需要访问联结表中的数据,我会按照您的建议为联结表使用 ActiveRecord
并使用 via()
:
If you need access to the data inside the junction table I would use an ActiveRecord
for the junction table as you suggested and use via()
:
class User extends ActiveRecord
{
public function getUserGroups() {
// one-to-many
return $this->hasMany(UserGroup::className(), ['user_id' => 'id']);
}
}
class Group extends ActiveRecord
{
public function getUserGroups() {
// one-to-many
return $this->hasMany(UserGroup::className(), ['group_id' => 'id']);
}
public function getUsers()
{
// many-to-many: uses userGroups relation above which uses an ActiveRecord class
return $this->hasMany(User::className(), ['id' => 'user_id'])
->via('userGroups');
}
}
class UserGroup extends ActiveRecord
{
public function getUser() {
// one-to-one
return $this->hasOne(User::className(), ['id' => 'user_id']);
}
public function getGroup() {
// one-to-one
return $this->hasOne(Group::className(), ['id' => 'userh_id']);
}
}
通过这种方式,您可以使用 userGroups
关系(与任何其他一对多关系一样)无需额外查询即可获取联结表的数据:
This way you can get the data of the junction table without additional queries using the userGroups
relation (like with any other one-to-many relation):
$group = Group::find()->where(['id' => $id])->with('userGroups.user')->one();
// --> 3 queries: find group, find user_group, find user
// $group->userGroups contains data of the junction table, for example:
$isAdmin = $group->userGroups[0]->adminFlag
// and the user is also fetched:
$userName = $group->userGroups[0]->user->name
这一切都可以使用 hasMany
关系来完成.所以你可能会问为什么要使用via()
来声明多对多关系:因为你可以使用Yii的link()
方法在联结表中创建项:
This all can be done using the hasMany
relation. So you may ask why you should declare the many-to-many relation using via()
: Because you can use Yii's link()
method to create items in the junction table:
$userGroup = new UserGroup();
// load data from form into $userGroup and validate
if ($userGroup->load(Yii::$app->request->post()) && $userGroup->validate()) {
// all data in $userGroup is valid
// --> create item in junction table incl. additional data
$group->link('users', $user, $userGroup->getDirtyAttributes())
}
相关文章