<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] >### **hasOne**('關聯模型(從表)類名', ' 從表外鍵', '當前模型(主表)主鍵') >### **belongsTo**('關聯模(主表)型類名','當前模型(從表)外鍵', '關聯(主表)主鍵'); > * 第二個參數外鍵形如user_id時 可省略 > * 第三個參數可省略 >[danger]一對一關系,存在主從關系(主表和從表 ),主表**不包含**外鍵**,**從表**包含**外鍵。 **hasOne** 和 **belongsTo** 都是一對一關系,區別: 在 **主表** (不包含外鍵)的模型中建立關聯關系,用 **hasOne**? 在 **從表**(包含外鍵)模型中建立關聯關系,用 **belongsTo** 用法: >[info]在當前模型下 申明一個**自定義函數**存放關聯定義(用于調用hasOne或者belongsTo并返回) >然后使用with或者hasWhere獲取最終關聯數據,上面定義的函數名作為這兩個方法的第一個參數傳入 例子: user(主表:關聯從表用hasOne): | id | name | status | --- | --- | --- | | 1 | thinkphp6.0 | 1 | | 2 | tom |1 | profile(從表:關聯主表用belongsTo): | id | uid | nickname|email| | --- | --- | --- |--- | | 1 | 1 | tp6.0 |123@123.com| # **在用戶表中關聯用戶詳情表(從)表** >[danger]## **hasOne('關聯模型類名', '外鍵', '主鍵');** ~~~ <?php namespace app\admin\model; use think\Model; class User extends Model { public function userProfile() { return $this->hasOne(Profile::class, 'uid');//如果第二個參數外鍵是user_id時框架能自動獲取到,就可以省略了 } } ~~~ 定義好關聯之后,就可以使用下面的**關聯查詢**方法獲取關聯數據: ~~~ $user = User::find(1); // 輸出Profile關聯模型的nickname屬性 echo $user->user_profile->nickname; ~~~ >[danger]**注意:** 關聯方法的命名規范是駝峰法(如上面的userProfile), 而關聯屬性則一般是小寫+下劃線的方式,系統在獲取的時候會自動轉換對應, 所以讀取`user_profile`關聯屬性則對應的關聯方法應該是`userProfile`。 ### **根據關聯數據查詢** 上面**關聯查詢**使用的是id,如果有其他條件呢? 可以根據關聯表的條件來查詢當前模型對象數據,例如: ~~~ <?php namespace \app\admin\model; use think\Model; class User extends Model { public function userProfile() { return $this->hasOne(Profile::class, 'uid'); } } ~~~ 上面在User里關聯profile數據表后我們就可以使用profile里的條件來查詢數據 ~~~ // 查詢用戶昵稱是think的用戶且是profile表的字段 // 注意第一個參數是上面關聯方法名(不是關聯模型名),hasWhere返回Query對象 $users = \app\admin\User::hasWhere('userProfile', ['nickname'=>'tom'])->select(); // 可以使用閉包查詢 $users = \app\admin\User::hasWhere('userProfile', function($query) { $query->where('nickname', 'like', 'think%'); })->select(); ~~~ ### **預載入查詢** 可以使用預載入查詢解決典型的[`N+1`]()查詢問題,使用: ~~~ $users = \app\admin\User::with('userProfile')->select(); foreach ($users as $user) { echo $user->user_profile->nickname; } ~~~ 怎么實現`$user->nickname;`也能獲取到profile表的nickname的值呢,可以通過下面的綁定屬性到父模型 ### **綁定屬性到父模型:bind()** ~~~ <?php namespace app\admin\model; use think\Model; class User extends Model { public function userProfile() { return $this->hasOne(Profile::class, 'uid')->bind(['nickname', 'email']); } } ~~~ >[info]或者指定綁定屬性別名 ~~~ <?php namespace app\admin\model; use think\Model; class User extends Model { public function userProfile() { return $this->hasOne(Profile::class, 'uid')->bind([ 'email', 'truename' => 'nickname', ]); } } ~~~ 然后使用關聯預載入查詢的時候,可以使用 ~~~ $user = \app\admin\User::with('userProfile')->find(1); // 直接輸出Profile關聯模型的綁定屬性 echo $user->email; echo $user->truename; ~~~ 綁定關聯模型的屬性支持讀取器。 > 如果不是預載入查詢,請使用模型的`appendRelationAttr`方法追加屬性。 也可以使用動態綁定關聯屬性,可以使用 ~~~ $user = User::find(1)->bindAttr('profile',['email','nickname']); // 輸出Profile關聯模型的email屬性 echo $user->email; echo $user->nickname; ~~~ 同樣支持指定屬性別名 ~~~ $user = User::find(1)->bindAttr('profile',[ 'email', 'truename' => 'nickname', ]); // 輸出Profile關聯模型的email屬性 echo $user->email; echo $user->truename; ~~~ 上面的代碼使用的是`IN`查詢,只會產生2條SQL查詢。如下: ``` SELECT * FROM `wy_user SELECT * FROM `wy_profile` WHERE `uid` IN (1,2,3,4,...) ``` >[danger] 由上可知wy_user表中的id必須存在于wy_profile表的uid中否則拋出致命錯誤,所以在這注意做好數據同步 >這里只針對一對一的情況,不管profileb表中存在多少uid為1的數據,通通只返回第一條uid為1的數據 如果要對關聯模型進行約束,可以使用閉包的方式。 ~~~ $users = \app\admin\User::with(['userProfile' => function($query) { $query->field('id,user_id,name,email'); }])->select(); foreach ($users as $user) { echo $user->profile->nickname; } ~~~ `with`方法可以傳入數組,表示同時對多個關聯模型(支持不同的關聯類型)進行預載入查詢。 ~~~ $users = \app\admin\User::with(['userProfile','book'])->select(); foreach ($users as $user) { echo $user->user_profile->name; foreach($user->book as $book) { echo $book->name; } } ~~~ with支持**嵌套查詢**:如 ~~~ //model/Banner.php namespace app\api\model; use think\Model; use think\Db; class Banner extends Model{ public function items(){ //banner關聯 Banner_item表 return $this->hasMany('BannerItem','banner_id','id'); } } //model/BannerItem.php namespace app\api\model; use think\Model; class BannerItem extends Model{ public function img(){ //bannner_item 關聯image表 return $this->hasOne('Image','img_id','id'); } } //model/Image.php namespace app\api\model; use think\Model; class Image extends Model{ } //在控制器方法中調用: $banner=\app\api\Model\Banner::with(['items','items.img'])->find($id); //多表關聯(參數是定義的方法名 ) //items 參數表明Banner類里有一個封裝關聯關系items方法 //items.img 表示items方法里關聯的表模型又關聯了一個封裝關聯關系的img方法 ~~~ 如果需要使用`JOIN`方式的查詢,直接使用`withJoin`方法,如下: ~~~ $users = \app\admin\User::withJoin('userProfile')->select(); foreach ($users as $user) { echo $user->user_profile->name; } ~~~ `withJoin`方法默認使用的是`INNER JOIN`方式,如果需要使用其它的,可以改成 ~~~ $users = \app\admin\User::withJoin('userProfile', 'LEFT')->select(); foreach ($users as $user) { echo $user->user_profile->name; } ~~~ >[danger]需要注意的是`withJoin`方式不支持嵌套關聯,通常你可以直接傳入多個需要關聯的模型。 如果需要約束關聯字段,可以使用下面的簡便方法。 ~~~ $users = \app\admin\User::withJoin([ 'userProfile' => ['user_id', 'nickname', 'email'] ])->select(); foreach ($users as $user) { echo $user->user_profile->nickname; } ~~~ ### **關聯保存** ~~~ $user = User::find(1); // 如果還沒有關聯數據 則進行新增 $user->userProfile()->save(['email' => 'thinkphp']); ~~~ 系統會自動把當前模型的主鍵傳入`Profile`模型。 和新增一樣使用`save`方法進行更新關聯數據。 ~~~ $user = User::find(1); $user->user_profile->email = 'thinkphp'; $user->user_profile->save(); // 或者 $user->user_profile->save(['email' => 'thinkphp']); ~~~ ## **關聯自動寫入** 我們可以使用`together`方法更方便的進行關聯自動寫入操作。 寫入 ~~~ $blog = new Blog; $blog->name = 'thinkphp'; $blog->title = 'ThinkPHP5關聯實例'; $content = new Content; $content->data = '實例內容'; $blog->content = $content; $blog->together(['content'])->save(); ~~~ 如果綁定了子模型的屬性到當前模型,可以指定子模型的屬性 ~~~ $blog = new Blog; $blog->name = 'thinkphp'; $blog->title = 'ThinkPHP5關聯實例'; $blog->content = '實例內容'; // title和content是子模型的屬性 $blog->together(['content'=>['title','content']])->save(); ~~~ 更新 ~~~ // 查詢 $blog = Blog::find(1); $blog->title = '更改標題'; $blog->content->data = '更新內容'; // 更新當前模型及關聯模型 $blog->together(['content'])->save(); ~~~ 刪除 ~~~ // 查詢 $blog = Blog::find(1,'content'); // 刪除當前及關聯模型 $blog->together(['content'])->delete(); ~~~ # **在用戶詳情表中關聯用戶(主)表 (定義相對關聯)** >[danger]## **hasOne('關聯模型類名', '外鍵', '主鍵');** ~~~ <?php namespace app\admin\model; use think\Model; class Profile extends Model { public function user() { return $this->belongsTo(User::class, 'uid');//外鍵如果是user_id格式則可以省略 } } ~~~ 我們就可以根據檔案資料來獲取用戶模型的信息 ~~~ $profile = \app\admin\Profile::find(1); // 輸出User關聯模型的屬性 echo $profile->user->name; ~~~
                  <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>

                              哎呀哎呀视频在线观看