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

      • <bdo id='9MFNB'></bdo><ul id='9MFNB'></ul>
      <tfoot id='9MFNB'></tfoot>

        <small id='9MFNB'></small><noframes id='9MFNB'>

        Laravel Eloquent ORM 中的多租户

        时间:2023-09-23

          <tbody id='AARES'></tbody>
        <legend id='AARES'><style id='AARES'><dir id='AARES'><q id='AARES'></q></dir></style></legend>
        • <tfoot id='AARES'></tfoot>

              <bdo id='AARES'></bdo><ul id='AARES'></ul>

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

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

                1. 本文介绍了Laravel Eloquent ORM 中的多租户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  这是来自

                  强制 Eloquent 模型重新解析数据库连接

                  有多个数据库连接:

                  return [
                      'default' => 'mysql-key1',
                      'connections' => [
                          'mysql-key1' => [
                              'driver'    => 'mysql',
                              'database'  => 'key1_dbname,
                              // etc
                          ],
                          'mysql-key2' => [
                              'driver'    => 'mysql',
                              'database'  => 'key2_dbname',
                              // etc
                          ]
                      ]
                  ];
                  

                  我有一个使用模型 setConnection 更改连接属性的产品存储库:

                  I have a product repository which uses the Model setConnection to change the connection attribute :

                  public function setConnection($name) {
                      // assumes $this->product is your Product model
                      $this->product->setConnection($name);
                  }
                  

                  然而,我发现它只适用于查询方法,而不适用于 Model::create ::

                  However, i found out that it only worked on query methods and not methods like Model::create ::

                  $productRepo = new ProductRepository();
                  foreach ($array as $key => $value) {
                     $productRepo->setConnection($key . '_' . $database);
                     // this works
                     $products = $productRepo->all();
                     // this doesn't work and will use the old connection 
                     $product = $productRepo->create($data); 
                  }
                  

                  似乎一旦创建了实例,::create 方法就不会解析连接.有人知道修复吗?

                  Seems that the ::create method does not resolve the connection once the instance has been created. Anyone knows a fix?

                  推荐答案

                  问题是 setConnection() 作用于一个类的实例,但是 create()> 方法是类本身的静态方法.在您的存储库中,$this->product 是 Product 类的一个实例.在执行查询之前在此实例上使用 setConnection() 会正常工作,但是如果您想在静态方法上使用单独的连接(例如 create()).

                  The problem is that setConnection() works on an instance of a class, but the create() method is a static method on the class itself. In your repository, $this->product is an instance of the Product class. Using setConnection() on this instance before doing queries will work fine, but you'll need to do a little more manual work if you want to use separate connections on the static methods (such as create()).

                  create() 方法所做的就是用给定的属性实例化一个新实例,然后调用 save().因此,无需在 Product 模型上调用 create(),您只需手动执行此操作:

                  All the create() method does is instantiate a new instance with the given attributes and then call save(). So, instead of calling create() on the Product model, you'll just need to do this manually:

                  class ProductRepository {
                      public function create(array $attributes, $connection = null) {
                          $product = $this->product->newInstance($attributes);
                          $product->setConnection($connection ?: $this->product->getConnectionName());
                          $product->save();
                          return $product;
                      }
                  }
                  

                  您还可以覆盖 Product 模型上的静态 create() 方法以接受连接.

                  You could also override the static create() method on the Product model to accept a connection, as well.

                  class Product extends Model {
                      public static function create(array $attributes, $connection = null) {
                          $model = new static($attributes);
                          $model->setConnection($connection ?: $this->connection);
                          $model->save();
                          return $model;
                      }
                  }
                  
                  class ProductRepository {
                      public function create(array $attributes, $connection = null) {
                          $connection = $connection ?: $this->product->getConnectionName()
                          return $this->product->create($attributes, $connection);
                      }
                  }
                  

                  这篇关于Laravel Eloquent ORM 中的多租户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在 Laravel 上更新数据透视表? 下一篇:防止 Laravel 将多条记录添加到数据透视表

                  相关文章

                  最新文章

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

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

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