<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智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ## 多對多關聯 |版本|功能調整| |---|---| |5.0.8|中間表名無需前綴,并支持定義中間表模型| |5.0.6|`attach`方法返回值改為`Pivot`對象| ## 關聯定義 例如,我們的用戶和角色就是一種多對多的關系,我們在User模型定義如下: ~~~ <?php namespace app\index\model; use think\Model; class User extends Model { public function roles() { return $this->belongsToMany('Role'); } } ~~~ belongsToMany方法的參數如下: >[info] ### belongsToMany('關聯模型名','中間表名','外鍵名','當前模型關聯鍵名',['模型別名定義']); `5.0.8+`版本開始,中間表名無需添加表前綴,并支持定義中間表模型,例如: ~~~ public function roles() { return $this->belongsToMany('Role','\app\index\model\Access'); } ~~~ ### 關聯查詢 我們可以通過下面的方式獲取關聯數據 ~~~ $user = User::get(1); // 獲取用戶的所有角色 dump($user->roles); ~~~ 如果要獲取中間表數據,可以使用 ~~~ $user = User::get(1); $roles = $user->roles; foreach($roles as $role){ // 獲取中間表數據 dump($role->pivot); } ~~~ ### 關聯新增 ~~~ $user = User::get(1); // 增加關聯數據 會自動寫入中間表數據 $user->roles()->save(['name'=>'管理員']); // 批量增加關聯數據 $user->roles()->saveAll([ ['name'=>'管理員'], ['name'=>'操作員'], ]); ~~~ 只新增中間表數據,可以使用 ~~~ $user = User::get(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]); ~~~ >[danger] `V5.0.6+`版本開始,`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>

                              哎呀哎呀视频在线观看