多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
#### :-: **数据表** ~~~ SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- 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=4 DEFAULT CHARSET=utf8mb4; -- ---------------------------- -- Records of tp_user -- ---------------------------- INSERT INTO `tp_user` VALUES ('1', '张三', '112222'); INSERT INTO `tp_user` VALUES ('2', '李四', '222222'); INSERT INTO `tp_user` VALUES ('3', '王麻子', '333333'); ~~~ #### :-: **中间表** ~~~ SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for `tp_user_book` -- ---------------------------- DROP TABLE IF EXISTS `tp_user_book`; CREATE TABLE `tp_user_book` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `user_id` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4; -- ---------------------------- -- Records of tp_user_book -- ---------------------------- INSERT INTO `tp_user_book` VALUES ('1', '1'); INSERT INTO `tp_user_book` VALUES ('2', '2'); INSERT INTO `tp_user_book` VALUES ('3', '3'); INSERT INTO `tp_user_book` VALUES ('4', '1'); INSERT INTO `tp_user_book` VALUES ('5', '2'); INSERT INTO `tp_user_book` VALUES ('6', '3'); ~~~ #### :-: **关联的表** ~~~ SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for `tp_book` -- ---------------------------- DROP TABLE IF EXISTS `tp_book`; CREATE TABLE `tp_book` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, `user_book_id` int(11) DEFAULT NULL, `status` int(11) DEFAULT '1', PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4; -- ---------------------------- -- Records of tp_book -- ---------------------------- INSERT INTO `tp_book` VALUES ('1', '乾坤大挪移', '1', '1'); INSERT INTO `tp_book` VALUES ('2', '六脉神剑', '2', '0'); INSERT INTO `tp_book` VALUES ('3', '如来神掌', '3', '1'); INSERT INTO `tp_book` VALUES ('4', '九阴真经', '1', '1'); INSERT INTO `tp_book` VALUES ('5', '降龙十八掌', '2', '1'); INSERT INTO `tp_book` VALUES ('6', '葵花宝典', '3', '1'); ~~~ #### :-: **user模型代码** ~~~ <?php namespace app\index\model; use think\Model; class UserModel extends Model { protected $table = 'tp_user'; protected $autoWriteTimestamp = true; //中间表依赖当前模型id-->关联模型依赖中间表id public function books() { // 关联模型(必须):模型名或者模型类名(存有中间表的id) //中间模型(必须):模型名或者模型类名(存有当前模型id) //外键:默认的外键名规则是当前模型名+_id //中间表关联键:默认的中间表关联键名的规则是中间模型名+_id //主键:当前模型主键,一般会自动获取也可以指定传入(当前模型id) return $this->hasManyThrough('BookModel','UserBookModel','user_id','user_book_id','id'); } } ~~~ #### :-: **控制器代码** ~~~ <?php namespace app\index\controller; use app\index\model\UserModel; use think\Controller; class Index extends Controller { public function index() { $db = new UserModel(); $user = $db->get('2'); // 获取user表id为2的数据 dump($user->books); // 也可以进行条件搜索 dump($user->books()->where('tp_book.status', 1)->select());//tp_book.status是tp_book表下面的status } } ~~~