<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國際加速解決方案。 廣告
                >[info] **從表建立關聯關系** >belongsTo('關聯主表模型名','從表外鍵名','關聯表主表鍵名',\['模型別名定義'\],'join類型'); >返回belongsTo類對象 | 參數 | 說明 | | --- | --- | | model | 關聯的從表模型名,該參數被定義時,與之對應當前模塊下必須有個與此參數名相同的模塊文件;如果不是前模塊而是跨模塊,此參數為帶有完整的命名空間的模型名 | | foreignKey | 從表外鍵名,默認為:當前模型名_id | | localKey | 主表的主鍵名 | | alias | 定義此模型的別名.`5.0.5+`版本開始,模型別名定義參數已經廢棄。 | | joinType | join類型,默認`INNER` | ## **定義** 定義一對一關聯,例如,一個用戶都有一個個人資料, user表(主表) ![](https://img.kancloud.cn/b8/10/b8105f74178cb588590809c2de5ce7e1_251x49.png) profile(從表) ![](https://img.kancloud.cn/04/68/0468a56482248422c8bca17d3980f272_367x43.png) **從建立關聯關系例子:** 定義主表對應的`Profile`模型如下: ~~~ namespace app\index\model; use think\Model; class Profile extends Model { public function profileRelationUser() { return $this->blongsTo('User'); //默認情況下, 我們使用的是`user_id`作為外鍵關聯,如果不是的話則需要在關聯定義的時候指定 //return $this->blongsTo('Profile','uid'); //`V5.0.3+`版本開始,可以支持為關聯模型定義需要查詢的字段;如果使用的是`join`方式的關聯,不支持指定field字段。 //return $this->blongsTo('Profile')->field('id,name,email'); //跨模塊需要完整的命名空間 //return $this->blongsTo('\app\api\Profile'); } } ~~~ ## **關聯查找** 在從表的Profile模型的profile方法定義好關聯之后,就可以使用下面的方法獲取關聯數據: ~~~ $profile=\app\index\model\Profile::get(1); echo $profile->profile_relation_user->id; echo $profile->profile_relation_user->username; echo $profile->phone; //或者 $user=\app\index\model\Profile::with('profile_relation_user')->find(1); echo $profile->profile_relation_user->id; echo $profile->profile_relation_user->username; echo $profile->phone; //with()返回的是Query對象 ~~~ >[danger] 有一點需要注意的是,關聯方法的命名規范是駝峰法,而關聯屬性則一般是小寫+下劃線的方式,系統在獲取的時候會自動轉換對應,讀取`user_profile`關聯屬性則對應的關聯方法應該是`userProfile`。 如果要根據關聯表的查詢條件查詢當前模型的數據,可以使用`hasWhere`方法,例如: ~~~ $user = \app\index\model\User::hasWhere('user_relation_profile',['phone'=>'0086-010-66778899'])->find(); echo $user->username; ~~~ ## **關聯新增** 參考主表一對一
                  <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>

                              哎呀哎呀视频在线观看