#### :-: **数据库表**
~~~
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_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', '1');
INSERT INTO `tp_book` VALUES ('3', '如来神掌', '3', '1');
INSERT INTO `tp_book` VALUES ('4', '九阴真经', '4', '1');
INSERT INTO `tp_book` VALUES ('5', '降龙十八掌', '5', '1');
INSERT INTO `tp_book` VALUES ('6', '葵花宝典', '6', '1');
-- ----------------------------
-- Table structure for `tp_profile`
-- ----------------------------
DROP TABLE IF EXISTS `tp_profile`;
CREATE TABLE `tp_profile` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`user_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb4;
-- ----------------------------
-- Records of tp_profile
-- ----------------------------
INSERT INTO `tp_profile` VALUES ('2', '李四的个人资料', '2');
INSERT INTO `tp_profile` VALUES ('3', '王五的个人资料', '3');
INSERT INTO `tp_profile` VALUES ('8', '赵六的个人资料', '6');
INSERT INTO `tp_profile` VALUES ('9', '张三的个人资料', '7');
-- ----------------------------
-- 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=9 DEFAULT CHARSET=utf8mb4;
-- ----------------------------
-- Records of tp_user
-- ----------------------------
INSERT INTO `tp_user` VALUES ('7', '张三', null);
INSERT INTO `tp_user` VALUES ('2', '李四', '222222');
INSERT INTO `tp_user` VALUES ('3', '王五', '333333');
INSERT INTO `tp_user` VALUES ('6', '赵六', null);
~~~
#### :-: **模型**
~~~
<?php
namespace app\index\model;
use think\Model;
class UserModel extends Model
{
protected $table = 'tp_user';
public function books()
{
return $this->hasOne('BookModel','user_id','id');
}
public function profile()
{
return $this->hasOne('ProfileModel','user_id','id');
}
~~~
~~~
<?php
namespace app\index\model;
use think\Model;
class ProfileModel extends Model
{
protected $table='tp_profile';
}
~~~
~~~
<?php
namespace app\index\model;
use think\Model;
class BookModel extends Model
{
protected $table = 'tp_book';
protected $autoWriteTimestamp = true;
}
~~~
#### :-: **控制器**
~~~
<?php
namespace app\index\controller;
use app\index\model\ProfileModel;
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
}
//根据关联条件来查询当前模型对象数据,返回当前模型数据
public function inde1()
{
$db = new UserModel();
$users = $db->hasWhere('books', ['name' => '六脉神剑'])->select();
dump($users);
}
//可以使用闭包查询
public function inde2()
{
$db = new UserModel();
$users = $db->hasWhere('books', function ($query) {
$query->where('name', 'like', '六脉神剑%');
})->select();
dump($users);
}
//预载入查询
//可以使用预载入查询解决典型的N+1查询问题,使用:
public function inde3()
{
$db = new UserModel();
$users = $db->with('books')->select();
dump($users);
foreach ($users as $user) {
echo $user->user_name."<br>";//查询主表的user_name字段
echo $user->books->name."<br>";//查询关联表的name字段
}
}
//对多个关联的表预载入查询
public function inde4()
{
$db = new UserModel();
$users = $db->with(['profile', 'books'])->select();
dump($users);
}
//新增数据到关联的表
public function inde5()
{
$db = new UserModel();
$user = $db->get(1);
// 如果还没有关联数据 则进行新增
$user->profile()->save(['name' => '赵六']);//数据新增加到profile表的name字段
}
//更新关联表数据
public function inde6()
{
$db = new UserModel();
$user = $db->get(1);
$user->profile->save(['name' => 'thinkphpp']);
}
//关联自动写入新的信息
public function inde7()
{
$user = new UserModel();
$user->user_name = '张三';//写入user表user_name字段
$user->user_pwd = '123456';
$profile = new ProfileModel();
$profile->name = '张三的个人资料';//写入profile表name字段
$user->profile = $profile;
$res = $user->together('profile')->save();
if ($res) {
return '添加成功';
}
return '添加失败';
}
//关联自动更新
public function inde8()
{
// 查询
$users = new UserModel();
$user = $users->get(1);
$user->user_name = '张三丰';//修改user表user_name字段的值
$user->profile->name = '张三丰的个人资料';
// 更新当前模型及关联模型
$user->together('profile')->save();
}
//关联自动删除
public function inde9()
{
// 查询
$users = new UserModel();
$user = $users->get(1,'profile');
// 删除当前及关联模型
$res =$user->together('profile')->delete();
if ($res){
return '删除成功';
}
return '删除失败';
}
}
~~~
### 定义相对关联
~~~
<?php
namespace app\index\model;
use think\Model;
class ProfileModel extends Model
{
protected $table='tp_profile';
public function user()
{
return $this->belongsTo('UserModel','user_id','id');
}
}
~~~
---------------------------------------------定义相对关联查询------------------------------------------------
~~~
//定义相对关联后查询数据
public function inde10()
{
$dbs=new ProfileModel();
$profile = $dbs->get(1);
// 输出User关联模型的属性
echo $profile->user->user_name;
}
~~~
## 绑定属性到父模型
~~~
<?php
namespace app\index\model;
use think\Model;
class UserModel extends Model
{
protected $table = 'tp_user';
protected $autoWriteTimestamp = true;
public function books()
{
return $this->hasOne('BookModel','user_id','id');
}
public function profile()
{
return $this->hasOne('ProfileModel','user_id','id')->bind('name');
}
}
~~~
---------------------------------------------绑定属性到父模型查询------------------------------
~~~
//关联模型字段绑定到主模型后输出
public function inde11()
{
$db = new UserModel();
$user = $db->get(1,'profile');
// 输出Profile关联模型的email属性
echo $user->name;
}
~~~