数据库中需要用到join连接查询时候,用thinkPHP框架的关联查询,可以有效的提高查询效率。(具体查询性能怎么样,咱也没试过,小伙伴们可以建两张数据量比较大的表,然后自己测试一下)
thinkPHP5手册:https://www.kancloud.cn/manual/thinkphp5/135196
hasOne: 一对一关联
这个没啥好解释的。数据之间的关联一对一。比如:一个用户表,一个最近登录表。我们设计之初,每个用户登录表只存储该用户最近一次的登录信息。
我们在SUser的模型中这样写两个方法:
public function sLogin()
{
return $this->hasOne('SLogin', 'user_id');
}
public function getLogin()
{
// SLogin 为关联表模型的名称。
return $this
->hasWhere('sLogin', ['create_at' => ['<', '2020-05-15 00:00:00']], 'SUser.last_login_ip, SLogin.visit_ip')
// ->with(['sLogin'])
->paginate(10);
}
控制器里面进行调用:
$result = (new SUser())->getRole();
return json($result);
就可以看到效果了。
注意:
模型中with和hasWhere的区别是:
使用hasWhere: 可以添加关联表的查询条件,并且生成数据的格式和with不同
(下同)
hasMany: 一对多关联
数据之间的关系一对多,比如:一个用户有多个用户资源。
同样是SUser模型:
public function uSource()
{
// 因为USource这个类不在当前命名空间,所以这样写。
return $this->hasMany('app\index\model\user\USource', 'user_id');
}
public function getSource()
{
return $this
->hasWhere('uSource', ['create_at' => ['<', '2020-05-01 00:00:00']], 'SUser.last_login_ip, USource.img_url')
// ->with(['uSource'])
->where('last_login_ip', '=', '127.0.0.1')
->paginate(10);
}
控制器不用写了吧。
belongsTo: 属于
当前方法的模型对应的数据表,属于另一个模型。相当于多对一。
比如:用户属于用户组。
public function sGroup()
{
return $this->belongsTo('SGroup', 'group_id');
}
public function getGroup()
{
return $this
->hasWhere('sGroup', [], 'SGroup.group_name, SUser.*')
// ->with(['sGroup'])
->where('last_login_ip', '=', '127.0.0.1')
->paginate();
}
belongsToMany: 多对多关联
这个也不用解释吧。就是数据之间的关系是多对多。
比如:一个用户可能又多个角色,一个角色又包含多个用户。所以他们会有一张中间表,将角色和用户关联起来。
public function userRoles()
{
// 第一个参数是当前模型所关联的另一个模型,第二个参数是关联中间表的表名,第三个参数是中间表中另一个模型的数据主键,最后一个就是当前模型关联中间表的主键。
// 条件如果需要包含中间表的字段,用wherePivot
return $this->belongsToMany('app\index\model\user\URoles', 's_relation', 'role_id', 'user_id')->wherePivot('type', 'eq', 1);
}
public function getRole()
{
return $this
->with(['userRoles'])
->where('last_login_ip', '=', '127.0.0.1')
->paginate(10);
}
也可以多张表一起关联:
public function getAll()
{
return $this
->with(['userRoles', 'sGroup', 'sLogin', 'uSource'])
->where('last_login_ip', '=', '127.0.0.1')
->paginate(10);
}
就这。没了。