#### :-: **數據表**
~~~
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
}
}
~~~