<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 關聯預載入 ## 關聯預載入 關聯查詢的預查詢載入功能,主要解決了`N+1`次查詢的問題,例如下面的查詢如果有3個記錄,會執行4次查詢: ``` $list = User::all([1,2,3]); foreach($list as $user){ // 獲取用戶關聯的profile模型數據 dump($user->profile); } ``` 如果使用關聯預查詢功能,就可以變成2次查詢(對于一對一關聯來說,如果使用JOIN方式只有一次查詢),有效提高性能。 ``` $list = User::with('profile')->select([1,2,3]); foreach($list as $user){ // 獲取用戶關聯的profile模型數據 dump($user->profile); } ``` 支持預載入多個關聯,例如: ``` $list = User::with('profile,book')->select([1,2,3]); ``` 也可以支持嵌套預載入,例如: ``` $list = User::with('profile.phone')->select([1,2,3]); foreach($list as $user){ // 獲取用戶關聯的phone模型 dump($user->profile->phone); } ``` 支持使用數組方式定義嵌套預載入,例如下面的預載入要同時獲取用戶的`Profile`關聯模型的`Phone`、`Job`和`Img`子關聯模型數據: ``` $list = User::with(['profile'=>['phone','job','img']])->select([1,2,3]); foreach($list as $user){ // 獲取用戶關聯 dump($user->profile->phone); dump($user->profile->job); dump($user->profile->img); } ``` 可以在模型的`get`和`all`方法中使用預載入,在第二個參數中傳入預載入信息即可。例如下面的用法和使用`with`方法加`select`方法是等效的: ``` $list = User::all([1,2,3],'profile,book'); ``` 如果要指定屬性查詢,可以使用: ``` $list = User::field('id,name')->with(['profile'=>function($query){ $query->field('email,phone'); }])->select([1,2,3]); foreach($list as $user){ // 獲取用戶關聯的profile模型數據 dump($user->profile); } ``` 關聯預載入名稱是關聯方法名,支持傳入方法名的小寫和下劃線定義方式,例如如果關聯方法名是`userProfile`和`userBook`的話: ``` $list = User::with('userProfile,userBook')->select([1,2,3]); ``` 等效于: ``` $list = User::with('user_profile,user_book')->select([1,2,3]); ``` 一對一關聯預載入支持兩種方式:`JOIN`方式(一次查詢)和`IN`方式(兩次查詢,默認方式),如果要使用`JOIN`方式關聯預載入,在關聯定義方法中添加 ``` <?php namespace app\index\model; use think\Model; class User extends Model { public function profile() { // 設置預載入查詢方式為JOIN方式 return $this->hasOne('Profile')->setEagerlyType(0); } } ``` 使用JOIN方式查詢的話 關聯定義的時候不能使用`field`方法指定字段,只能在預載入查詢的時候使用`withField`方法指定字段,例如: ``` $list = User::with(['profile' => function($query){ $query->withField('truename,email'); }])->select([1,2,3]); ``` ### 延遲預載入 有些情況下,需要根據查詢出來的數據來決定是否需要使用關聯預載入,當然關聯查詢本身就能解決這個問題,因為關聯查詢是惰性的,不過用預載入的理由也很明顯,性能具有優勢。 延遲預載入僅針對多個數據的查詢,因為單個數據的查詢用延遲預載入和關聯惰性查詢沒有任何區別,所以不需要使用延遲預載入。 如果你的數據集查詢返回的是數據集對象,可以使用調用數據集對象的`load`實現延遲預載入: ``` // 查詢數據集 $list = User::all([1,2,3]); // 延遲預載入 $list->load('cards'); foreach($list as $user){ // 獲取用戶關聯的card模型數據 dump($user->cards); } ```
                  <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>

                              哎呀哎呀视频在线观看