1. <legend id='r13dd'><style id='r13dd'><dir id='r13dd'><q id='r13dd'></q></dir></style></legend>

      • <bdo id='r13dd'></bdo><ul id='r13dd'></ul>

      <small id='r13dd'></small><noframes id='r13dd'>

        <i id='r13dd'><tr id='r13dd'><dt id='r13dd'><q id='r13dd'><span id='r13dd'><b id='r13dd'><form id='r13dd'><ins id='r13dd'></ins><ul id='r13dd'></ul><sub id='r13dd'></sub></form><legend id='r13dd'></legend><bdo id='r13dd'><pre id='r13dd'><center id='r13dd'></center></pre></bdo></b><th id='r13dd'></th></span></q></dt></tr></i><div id='r13dd'><tfoot id='r13dd'></tfoot><dl id='r13dd'><fieldset id='r13dd'></fieldset></dl></div>

        <tfoot id='r13dd'></tfoot>

        从 Yii2 中的连接表中检索数据

        时间:2023-10-16

          • <tfoot id='qrMwP'></tfoot>

            <i id='qrMwP'><tr id='qrMwP'><dt id='qrMwP'><q id='qrMwP'><span id='qrMwP'><b id='qrMwP'><form id='qrMwP'><ins id='qrMwP'></ins><ul id='qrMwP'></ul><sub id='qrMwP'></sub></form><legend id='qrMwP'></legend><bdo id='qrMwP'><pre id='qrMwP'><center id='qrMwP'></center></pre></bdo></b><th id='qrMwP'></th></span></q></dt></tr></i><div id='qrMwP'><tfoot id='qrMwP'></tfoot><dl id='qrMwP'><fieldset id='qrMwP'></fieldset></dl></div>
            <legend id='qrMwP'><style id='qrMwP'><dir id='qrMwP'><q id='qrMwP'></q></dir></style></legend>
                <tbody id='qrMwP'></tbody>

              <small id='qrMwP'></small><noframes id='qrMwP'>

                • <bdo id='qrMwP'></bdo><ul id='qrMwP'></ul>
                  本文介绍了从 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())
                  }
                  

                  这篇关于从 Yii2 中的连接表中检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 yii2 中自定义网格视图 下一篇:尝试在 ec2 上安装 php-mbstring 的依赖问题

                  相关文章

                  • <bdo id='mzU2N'></bdo><ul id='mzU2N'></ul>
                  <legend id='mzU2N'><style id='mzU2N'><dir id='mzU2N'><q id='mzU2N'></q></dir></style></legend>

                  1. <i id='mzU2N'><tr id='mzU2N'><dt id='mzU2N'><q id='mzU2N'><span id='mzU2N'><b id='mzU2N'><form id='mzU2N'><ins id='mzU2N'></ins><ul id='mzU2N'></ul><sub id='mzU2N'></sub></form><legend id='mzU2N'></legend><bdo id='mzU2N'><pre id='mzU2N'><center id='mzU2N'></center></pre></bdo></b><th id='mzU2N'></th></span></q></dt></tr></i><div id='mzU2N'><tfoot id='mzU2N'></tfoot><dl id='mzU2N'><fieldset id='mzU2N'></fieldset></dl></div>

                      <tfoot id='mzU2N'></tfoot>

                    1. <small id='mzU2N'></small><noframes id='mzU2N'>