<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                `Customer.php`: ~~~ class Customer extends \yii\db\ActiveRecord { public function getOrder() { //第一個customer_id是Order表中的字段,第二個customer_id是Customer表中的字段 //比如查詢1號訂單的顧客資料,1號訂單的customer_id=1 //然后在Customer表中查詢customer_id=1的顧客資料 return $this->hasMany(Order::className(),['customer_id' => 'customer_id'])->asArray()->all(); } } ~~~ * * * * * `Order.php` ~~~ class Order extends \yii\db\ActiveRecord { public function getCustomer() { //第一個customer_id是Customer表中的字段,第二個customer_id是Order表中的字段 //比如查詢zhangsan的訂單,zhangsan的customer_id=1 //然后在Order表中查詢customer_id=1的訂單 return $this->hasOne(Customer::className(),['customer_id' => 'customer_id'])->asArray()->one(); } } ~~~ * * * * * 控制器的方法: ~~~ function actionOrder() { /******************************* 關聯查詢 ****************************/ /********** 1、根據顧客名字查詢顧客訂單,顧客1:n訂單 ******/ //查詢zhangsan的訂單 $customer = Customer::find()->where(['name' => 'zhangsan'])->one(); $order = $customer->getOrder(); print_r($order); /********** 2、根據訂單查詢顧客名字,訂單1:1顧客 *********/ //查詢1號訂單的顧客資料 $order = Order::find()->where(['order_id' => 1])->one(); $customer = $order->getCustomer(); print_r($customer); /**** $customer = $orders->customer; * 3、當訪問不存在的屬性時$orders->customer,php會自動調用__get() * 即$orders->customer相當于調用$orders->getCustomer() * 同時還會在末尾自動補上one()方法,即$orders->getCustomer()->one() * 因此,當你使用$orders->customer時,要在getCustomer()方法末尾要去掉one() *****/ /********************* 關聯查詢問題1:關聯查詢結果會被緩存 **********************/ /* 當你執行 $order=Order::find()->where(['order_id' => 1])->one() 時,往數據表進行了查詢 * 第二次執行 $order=Order::find()->where(['order_id' => 1])->one() 時則不會從數據表中進行查詢,會直接從緩存中讀取 * 所以當你更新數據表的時候,你查詢到的結果卻沒有更新,此時你需要先unset($order)刪除緩存再從數據表中查詢 */ /********************* 關聯查詢問題2:關聯查詢的多次查詢 **********************/ $customers=Customer::find()->all(); //1次sql語句select * from customer foreach($customers as $customer){ //如果Customer表中有100條記錄,則會執行100次sql語句select * from order where customer_id=... print_r($customer->order); } /*解決方法:加上with('order') * select * from customer,select * from order where customer_id in(...),這時的...表示所有顧客customer_id的集合 * 選取所有顧客的customer_id塞到各自的order屬性,執行下面foreach時不會再有sql語句,這里只執行了2次sql語句*/ /*$customers=Customer::find()->with('order')->all(); foreach($customers as $customer){ print_r($customer->order); }*/ } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看