<legend id='fh0b0'><style id='fh0b0'><dir id='fh0b0'><q id='fh0b0'></q></dir></style></legend>

  • <tfoot id='fh0b0'></tfoot>
  • <small id='fh0b0'></small><noframes id='fh0b0'>

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

        我如何在 Yii2 中处理多对多关系

        时间:2023-10-16
          <tbody id='hC40i'></tbody>

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

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

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

                <bdo id='hC40i'></bdo><ul id='hC40i'></ul>
                1. <legend id='hC40i'><style id='hC40i'><dir id='hC40i'><q id='hC40i'></q></dir></style></legend>
                2. 本文介绍了我如何在 Yii2 中处理多对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  For example in one-to-many due to documentation (http://www.yiiframework.com/doc-2.0/guide-db-active-record.html#relational-data) you can link two models in this way (one-many = company-zone):

                  $defaultZone = new Zone;
                  $defaultZone->name = Zone::DEFAULT_ZONE;
                  $company->link('zones', $defaultZone);
                  

                  But how it works for many-to-many relations when you have transit table like tbl_user_market(user_id, market_id)?

                  解决方案

                  When using a junction table for many-to-many relations, you have to

                  1. Define the relations
                  2. Link the two models together

                  In the User model define the following relation function:

                  public function getMarkets() {
                      return $this->hasMany(Market::className(), ['id' => 'market_id'])
                        ->viaTable('tbl_user_market', ['user_id' => 'id']);
                  }
                  

                  In the Market model define the following relation function:

                  public function getUsers() {
                      return $this->hasMany(User::className(), ['id' => 'user_id'])
                        ->viaTable('tbl_user_market', ['market_id' => 'id']);
                  }
                  

                  And finally, after saving both models, link them together:

                  $user = new User;
                  $user->name = 'Foo';
                  $user->save();
                  
                  $market = new Market;
                  $market->name = 'Bar';
                  $market->save();
                  
                  $user->link('markets', $market);
                  

                  The call to link() will populate the junction table.

                  Reference: http://www.yiiframework.com/doc-2.0/yii-db-baseactiverecord.html#link()-detail

                  这篇关于我如何在 Yii2 中处理多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Yii2 : ActiveQuery Example 以及在 Gii 中单独生成 ActiveQuery 类的原因是什么 下一篇:如何将参数从控制器传递到 YII2 中的布局

                  相关文章

                    <bdo id='DXVMk'></bdo><ul id='DXVMk'></ul>
                3. <small id='DXVMk'></small><noframes id='DXVMk'>

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

                      <legend id='DXVMk'><style id='DXVMk'><dir id='DXVMk'><q id='DXVMk'></q></dir></style></legend>