<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 中处理多对多关系

        How do I work with many-to-many relations in Yii2(我如何在 Yii2 中处理多对多关系)
          <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 中处理多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Is Joomla 2.5 much faster than Joomla 1.5 Querywise(Joomla 2.5 比 Joomla 1.5 Querywise 快得多吗)
                  How to share Joomla login session from one joomla website to one ASP.Net MVC website(如何将 Joomla 登录会话从一个 joomla 网站共享到一个 ASP.Net MVC 网站)
                  htaccess redirect root to subdirectory but allow index.php in root AND query strings to function(htaccess 将根重定向到子目录,但允许根和查询字符串中的 index.php 起作用)
                  Joomla include database functions(Joomla 包含数据库功能)
                  nl2br() not working when displaying SQL results(显示 SQL 结果时 nl2br() 不起作用)
                  Joomla 2.5 JFactory::getSession(); seems to be caching in firefox(Joomla 2.5 JFactory::getSession();似乎在 Firefox 中缓存)
                    <bdo id='DXVMk'></bdo><ul id='DXVMk'></ul>
                  • <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>

                        <tbody id='DXVMk'></tbody>

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