pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

<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 class="zn7dfnt" 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 中的多租戶

        Multi tenancy in Laravel Eloquent ORM(Laravel Eloquent ORM 中的多租戶)

        <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 class="1nf5xzt" 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'>

                    <tbody id='9MFNB'></tbody>

                  本文介紹了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模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  MySQLi prepared statement amp; foreach loop(MySQLi準備好的語句amp;foreach 循環)
                  Is mysqli_insert_id() gets record from whole server or from same user?(mysqli_insert_id() 是從整個服務器還是從同一用戶獲取記錄?)
                  PHP MySQLi doesn#39;t recognize login info(PHP MySQLi 無法識別登錄信息)
                  mysqli_select_db() expects exactly 2 parameters(mysqli_select_db() 需要 2 個參數)
                  Php mysql pdo query: fill up variable with query result(Php mysql pdo 查詢:用查詢結果填充變量)
                  MySQLI 28000/1045 Access denied for user #39;root#39;@#39;localhost#39;(MySQLI 28000/1045 用戶“root@“localhost的訪問被拒絕)

                  <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 class="dvhvbx7" id='Tb6SW'><tfoot id='Tb6SW'></tfoot><dl id='Tb6SW'><fieldset id='Tb6SW'></fieldset></dl></div>

                          <tbody id='Tb6SW'></tbody>

                          <bdo id='Tb6SW'></bdo><ul id='Tb6SW'></ul>
                          <legend id='Tb6SW'><style id='Tb6SW'><dir id='Tb6SW'><q id='Tb6SW'></q></dir></style></legend>
                            主站蜘蛛池模板: 翰墨AI智能写作助手官网_人工智能问答在线AI写作免费一键生成 | 浙江筋膜枪-按摩仪厂家-制造商-肩颈按摩仪哪家好-温州市合喜电子科技有限公司 | 小型气象站_便携式自动气象站_校园气象站-竞道气象设备网 | 红外光谱仪维修_二手红外光谱仪_红外压片机_红外附件-天津博精仪器 | 水质监测站_水质在线分析仪_水质自动监测系统_多参数水质在线监测仪_水质传感器-山东万象环境科技有限公司 | 艺术涂料_进口艺术涂料_艺术涂料加盟_艺术涂料十大品牌 -英国蒙太奇艺术涂料 | 南京技嘉环保科技有限公司-杀菌除臭剂|污水|垃圾|厕所|橡胶厂|化工厂|铸造厂除臭剂 | 置顶式搅拌器-优莱博化学防爆冰箱-磁驱搅拌器-天津市布鲁克科技有限公司 | 密集柜_档案密集柜_智能密集架_密集柜厂家_密集架价格-智英伟业 密集架-密集柜厂家-智能档案密集架-自动选层柜订做-河北风顺金属制品有限公司 | 科箭WMS仓库管理软件-TMS物流管理系统-科箭SaaS云服务 | 仿清水混凝土_清水混凝土装修_施工_修饰_保护剂_修补_清水混凝土修复-德州忠岭建筑装饰工程 | 山东太阳能路灯厂家-庭院灯生产厂家-济南晟启灯饰有限公司 | OpenI 启智 新一代人工智能开源开放平台 | 色谱柱-淋洗液罐-巴罗克试剂槽-巴氏吸管-5ml样品瓶-SBS液氮冻存管-上海希言科学仪器有限公司 | 杭州中央空调维修_冷却塔/新风机柜/热水器/锅炉除垢清洗_除垢剂_风机盘管_冷凝器清洗-杭州亿诺能源有限公司 | 二手色谱仪器,十万分之一分析天平,蒸发光检测器,电位滴定仪-湖北捷岛科学仪器有限公司 | 活性氧化铝球|氧化铝干燥剂|分子筛干燥剂|氢氧化铝粉-淄博同心材料有限公司 | 仓储笼_金属箱租赁_循环包装_铁网箱_蝴蝶笼租赁_酷龙仓储笼租赁 测试治具|过炉治具|过锡炉治具|工装夹具|测试夹具|允睿自动化设备 | 湖州织里童装_女童男童中大童装_款式多尺码全_织里儿童网【官网】-嘉兴嘉乐网络科技有限公司 | 丹尼克尔拧紧枪_自动送钉机_智能电批_柔性振动盘_螺丝供料器品牌 | 波纹补偿器_不锈钢波纹补偿器_巩义市润达管道设备制造有限公司 | uv固化机-丝印uv机-工业烤箱-五金蚀刻机-分拣输送机 - 保定市丰辉机械设备制造有限公司 | 国际船舶网 - 船厂、船舶、造船、船舶设备、航运及海洋工程等相关行业综合信息平台 | 液压油缸-液压站生产厂家-洛阳泰诺液压科技有限公司 | 水稻烘干机,小麦烘干机,大豆烘干机,玉米烘干机,粮食烘干机_巩义市锦华粮食烘干机械制造有限公司 水环真空泵厂家,2bv真空泵,2be真空泵-淄博真空设备厂 | 阴离子聚丙烯酰胺价格_PAM_高分子聚丙烯酰胺厂家-河南泰航净水材料有限公司 | 泥浆在线密度计厂家-防爆数字压力表-膜盒-远传压力表厂家-江苏大亚自控设备有限公司 | 交流伺服电机|直流伺服|伺服驱动器|伺服电机-深圳市华科星电气有限公司 | 12cr1mov无缝钢管切割-15crmog无缝钢管切割-40cr无缝钢管切割-42crmo无缝钢管切割-Q345B无缝钢管切割-45#无缝钢管切割 - 聊城宽达钢管有限公司 | 雷达液位计_超声波风速风向仪_雨量传感器_辐射传感器-山东风途物联网 | 焊接减速机箱体,减速机箱体加工-淄博博山泽坤机械厂 | 开锐教育-学历提升-职称评定-职业资格培训-积分入户 | 据信,上课带着跳 D 体验-别样的课堂刺激感受引发网友热议 | 道达尔润滑油-食品级润滑油-道达尔导热油-合成导热油,深圳道达尔代理商合-深圳浩方正大官网 | 科昊仪器超纯水机系统-可成气相液氮罐-美菱超低温冰箱-西安昊兴生物科技有限公司 | 东亚液氮罐-液氮生物容器-乐山市东亚机电工贸有限公司 | 执业药师报名时间,报考条件,考试时间-首页入口 | 鼓风干燥箱_真空烘箱_高温干燥箱_恒温培养箱-上海笃特科学仪器 | 精密机械零件加工_CNC加工_精密加工_数控车床加工_精密机械加工_机械零部件加工厂 | 世纪豪门官网 世纪豪门集成吊顶加盟电话 世纪豪门售后电话 | 地磅-地秤-江阴/无锡地磅-江阴天亿计量设备有限公司_ |