<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>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                #### :-: **模型** ~~~ <?php namespace app\index\model; use think\Model; class UserModel extends Model { protected $table = 'tp_user'; protected $autoWriteTimestamp = true; public function roles(){ return $this->belongsToMany('RoleModel','\\app\\index\\model\\UserRoleModel','role_id','user_id'); } } ~~~ ~~~ <?php namespace app\index\model; use think\Model; class RoleModel extends Model { protected $table='tp_role'; } ~~~ ~~~ <?php namespace app\index\model; use think\Model; use think\model\Pivot; class UserRoleModel extends Pivot { protected $table='tp_user_role'; } ~~~ #### :-: **數據表** ~~~ SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for `tp_role` -- ---------------------------- DROP TABLE IF EXISTS `tp_role`; CREATE TABLE `tp_role` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, `status` int(11) DEFAULT '1', PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=12 DEFAULT CHARSET=utf8mb4; -- ---------------------------- -- Records of tp_role -- ---------------------------- INSERT INTO `tp_role` VALUES ('1', '管理員', '1'); INSERT INTO `tp_role` VALUES ('2', '審核員', '1'); INSERT INTO `tp_role` VALUES ('3', '訂單審核員', '1'); -- ---------------------------- -- Table structure for `tp_user` -- ---------------------------- DROP TABLE IF EXISTS `tp_user`; CREATE TABLE `tp_user` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `user_name` varchar(255) DEFAULT NULL, `user_pwd` int(32) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4; -- ---------------------------- -- Records of tp_user -- ---------------------------- INSERT INTO `tp_user` VALUES ('1', '張三', '222222'); INSERT INTO `tp_user` VALUES ('2', '李四', '222222'); INSERT INTO `tp_user` VALUES ('3', '王五', '333333'); INSERT INTO `tp_user` VALUES ('4', '趙六', '222222'); -- ---------------------------- -- Table structure for `tp_user_role` -- ---------------------------- DROP TABLE IF EXISTS `tp_user_role`; CREATE TABLE `tp_user_role` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `user_id` int(11) DEFAULT NULL, `role_id` int(11) DEFAULT NULL, `remark` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=33 DEFAULT CHARSET=utf8mb4; -- ---------------------------- -- Records of tp_user_role -- ---------------------------- INSERT INTO `tp_user_role` VALUES ('28', '1', '1', null); INSERT INTO `tp_user_role` VALUES ('27', '1', '1', null); INSERT INTO `tp_user_role` VALUES ('26', '1', '1', null); INSERT INTO `tp_user_role` VALUES ('29', '1', '1', 'test'); INSERT INTO `tp_user_role` VALUES ('30', '1', '1', null); INSERT INTO `tp_user_role` VALUES ('31', '1', '2', 'test'); INSERT INTO `tp_user_role` VALUES ('32', '1', '2', null); ~~~ #### :-: **控制器** ~~~ <?php namespace app\index\controller; use app\index\model\RoleModel; use app\index\model\UserModel; use think\Controller; use think\Request; class Test1 extends Controller { //關聯查詢 public function index() { $db = new UserModel(); $user = $db->get(1); // 獲取用戶的所有角色 $roles = $user->roles; foreach ($roles as $role) { // 輸出用戶的角色名 echo $role->name; // 獲取中間表模型 dump($role->pivot); } } //關聯新增 public function index1() { $db = new UserModel(); $user = $db->get(1); // 給用戶增加管理員權限 會自動寫入角色表和中間表數據 $user->roles()->save(['name' => '管理']); // 批量授權 $user->roles()->saveAll([ ['name' => '管理員'], ['name' => '操作員'], ]); } //只新增中間表數據(角色已經提前創建完成),可以使用 public function index2() { $db = new UserModel(); $user = $db->get(1); // 僅增加管理員權限(假設管理員的角色ID是1) //$user->roles()->save(2); // 或者 $dbs = new RoleModel(); $role = $dbs->get(1); $user->roles()->save($role); // 批量增加關聯數據 $user->roles()->saveAll([1, 2, 3]); } //單獨更新中間表數據,可以使用: public function index3() { $db = new UserModel(); $user = $db->get(1); // 增加關聯的中間表數據 $user->roles()->attach(2); // 傳入中間表的額外屬性 //$user->roles()->attach(2,['remark'=>'test']); // 刪除中間表數據 //$user->roles()->detach([1,2,3]); } } ~~~
                  <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>

                              哎呀哎呀视频在线观看