<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智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 多對多關聯 ## 多對多關聯 ## 關聯定義 例如,我們的用戶和角色就是一種多對多的關系,我們在`User`模型定義如下: ``` <?php namespace app\index\model; use think\Model; class User extends Model { public function roles() { return $this->belongsToMany('Role'); } } ``` `belongsToMany`方法的參數如下: > ### belongsToMany('關聯模型','中間表','外鍵','關聯鍵'); - **關聯模型**(必須):模型名或者模型類名 - **中間表**:默認規則是當前模型名+`_`+關聯模型名 (可以指定模型名) - **外鍵**:中間表的當前模型外鍵,默認的外鍵名規則是關聯模型名+`_id` - **關聯鍵**:中間表的當前模型關聯鍵名,默認規則是當前模型名+`_id` 中間表名無需添加表前綴,并支持定義中間表模型,例如: ``` public function roles() { return $this->belongsToMany('Role','\\app\\model\\Access'); } ``` 中間表模型類必須繼承`think\model\Pivot`,例如: ``` <?php namespace app\index\model\Access; use think\model\Pivot; class Access extends Pivot { protected $autoWriteTimestamp = true; } ``` 中間表模型的基類`Pivot`默認關閉了時間戳自動寫入,上面的中間表模型則開啟了時間戳字段自動寫入。 ### 關聯查詢 我們可以通過下面的方式獲取關聯數據 ``` $user = User::get(1); // 獲取用戶的所有角色 $roles = $user->roles; foreach ($roles as $role) { // 輸出用戶的角色名 echo $role->name; // 獲取中間表模型 dump($role->pivot); } ``` ### 關聯新增 ``` $user = User::get(1); // 給用戶增加管理員權限 會自動寫入角色表和中間表數據 $user->roles()->save(['name'=>'管理員']); // 批量授權 $user->roles()->saveAll([ ['name'=>'管理員'], ['name'=>'操作員'], ]); ``` 只新增中間表數據(角色已經提前創建完成),可以使用 ``` $user = User::get(1); // 僅增加管理員權限(假設管理員的角色ID是1) $user->roles()->save(1); // 或者 $role = Role::get(1); $user->roles()->save($role); // 批量增加關聯數據 $user->roles()->saveAll([1,2,3]); ``` 單獨更新中間表數據,可以使用: ``` $user = User::get(1); // 增加關聯的中間表數據 $user->roles()->attach(1); // 傳入中間表的額外屬性 $user->roles()->attach(1,['remark'=>'test']); // 刪除中間表數據 $user->roles()->detach([1,2,3]); ``` > `attach`方法的返回值是一個`Pivot`對象實例,如果是附加多個關聯數據,則返回`Pivot`對象實例的數組。 ### 定義相對的關聯 我們可以在`Role`模型中定義一個相對的關聯關系,例如: ``` <?php namespace app\index\model; use think\Model; class Role extends Model { public function users() { return $this->belongsToMany('User'); } } ```
                  <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>

                              哎呀哎呀视频在线观看