多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
#### :-: **模型** ~~~ <?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]); } } ~~~