<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國際加速解決方案。 廣告
                # 一對一關聯 ## 一對一關聯 版本新增功能5.1.2增加`selfRelation`方法定義當前關聯為自關聯### 關聯定義 定義一對一關聯,例如,一個用戶都有一個個人資料,我們定義`User`模型如下: ``` <?php namespace app\index\model; use think\Model; class User extends Model { public function profile() { return $this->hasOne('Profile'); } } ``` `hasOne`方法的參數包括: > ### hasOne('關聯模型','外鍵','主鍵'); 除了關聯模型外,其它參數都是可選。 - **關聯模型**(必須):關聯的模型名或者類名 - **外鍵**:默認的外鍵規則是當前模型名(不含命名空間,下同)+`_id` ,例如`user_id` - **主鍵**:當前模型主鍵,默認會自動獲取也可以指定傳入 一對一關聯定義的時候還支持額外的方法,包括: 方法名描述setEagerlyType定義關聯查詢方式,0 - `JOIN`查詢 1 - `IN`查詢(默認)bind綁定關聯屬性到父模型joinTypeJOIN方式查詢的JOIN方式,默認為`INNER`selfRelation定義當前關聯為自關聯> 如果使用了JOIN方式的關聯查詢方式,你可以在額外的查詢條件中使用關聯方法名作為表的別名。 ### 關聯查詢 定義好關聯之后,就可以使用下面的方法獲取關聯數據: ``` $user = User::get(1); // 輸出Profile關聯模型的email屬性 echo $user->profile->email; ``` 默認情況下, 我們使用的是`user_id` 作為外鍵關聯,如果不是的話則需要在關聯定義的時候指定,例如: ``` <?php namespace app\index\model; use think\Model; class User extends Model { public function profile() { return $this->hasOne('Profile','uid'); } } ``` > 有一點需要注意的是,關聯方法的命名規范是駝峰法,而關聯屬性則一般是小寫+下劃線的方式,系統在獲取的時候會自動轉換對應,讀取`user_profile`關聯屬性則對應的關聯方法應該是`userProfile`。 ### 關聯保存 ``` $user = User::get(1); // 如果還沒有關聯數據 則進行新增 $user->profile()->save(['email' => 'thinkphp']); ``` 系統會自動把當前模型的主鍵傳入`Profile`模型。 和新增一樣使用`save`方法進行更新關聯數據。 ``` $user = User::get(1); $user->profile->email = 'thinkphp'; $user->profile->save(); // 或者 $user->profile->save(['email' => 'thinkphp']); ``` ### 定義相對關聯 我們可以在`Profile`模型中定義一個相對的關聯關系,例如: ``` <?php namespace app\index\model; use think\Model; class Profile extends Model { public function user() { return $this->belongsTo('User'); } } ``` `belongsTo`的參數包括: > ### belongsTo('關聯模型','外鍵','關聯主鍵'); 除了關聯模型外,其它參數都是可選。 - **關聯模型**(必須):模型名或者模型類名 - **外鍵**:當前模型外鍵,默認的外鍵名規則是關聯模型名+`_id` - **關聯主鍵**:關聯模型主鍵,一般會自動獲取也可以指定傳入 默認的關聯外鍵是`user_id`,如果不是,需要在第二個參數定義 ``` <?php namespace app\index\model; use think\Model; class Profile extends Model { public function user() { return $this->belongsTo('User','uid'); } } ``` 我們就可以根據檔案資料來獲取用戶模型的信息 ``` $profile = Profile::get(1); // 輸出User關聯模型的屬性 echo $profile->user->account; ``` ## 綁定屬性到父模型 可以在定義關聯的時候使用`bind`方法綁定屬性到父模型,例如: ``` <?php namespace app\index\model; use think\Model; class User extends Model { public function profile() { return $this->hasOne('Profile','uid')->bind('nickname,email'); } } ``` 或者使用數組的方式指定綁定屬性別名 ``` <?php namespace app\index\model; use think\Model; class User extends Model { public function profile() { return $this->hasOne('Profile','uid')->bind([ 'email', 'truename' => 'nickname', 'profile_id' => 'id', ]); } } ``` 然后使用關聯預載入查詢的時候,可以使用 ``` $user = User::get(1,'profile'); // 輸出Profile關聯模型的email屬性 echo $user->email; echo $user->profile_id; ``` 綁定關聯模型的屬性支持讀取器。 > 如果不是預載入查詢,請使用模型的`appendRelationAttr`方法追加屬性。 ## 關聯自動寫入 我們可以使用`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::get(1); $blog->title = '更改標題'; $blog->content->data = '更新內容'; // 更新當前模型及關聯模型 $blog->together('content')->save(); ``` 刪除 ``` // 查詢 $blog = Blog::get(1,'content'); // 刪除當前及關聯模型 $blog->together('content')->delete(); ``` > 如果不想這么麻煩每次調用`together`方法,也可以直接在模型類中定義`relationWrite`屬性,但必須是數組方式。不過考慮到模型的獨立操作的可能性,并不建議。
                  <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>

                              哎呀哎呀视频在线观看