模型关联
避免在模型内部使用复杂的
join
查询和视图查询。一个模型可以定义多个不同的关联,增加不同的关联方法即可
推荐关联属性统一使用和数据表的字段命名规范一致,在很多时候系统自动获取关联属性的时候采用的也是。
模型方法 | 关联类型 |
---|---|
hasOne | 一对一 |
belongsTo | 一对一 |
hasMany | 一对多 |
hasManyThrough | 远程一对多 |
belongsToMany | 多对多 |
morphMany | 多态一对多 |
morphOne | 多态一对一 |
morphTo | 多态 |
类型 | 关联关系 | 相对的关联关系 |
---|---|---|
一对一 | hasOne | belongsTo |
一对多 | hasMany | belongsTo |
多对多 | belongsToMany | belongsToMany |
远程一对多 | hasManyThrough | 不支持 |
多态一对一 | morphOne | morphTo |
多态一对多 | morphMany | morphTo |
一对一关联
hasOne('关联模型','外键','主键');
除了关联模型外,其它参数都是可选。
- 关联模型(必须):关联的模型名或者类名
- 外键:默认的外键规则是当前模型名(不含命名空间,下同)+
_id
,例如user_id
- 主键:当前模型主键,默认会自动获取也可以指定传入
一、关联查询
<?php
namespace app\index\model;
use think\Model;
class User extends Model
{
public function profile()
{
return $this->hasOne('Profile','uid');
}
}
//获取数据
$user = User::get(1);
// 输出Profile关联模型的email属性
echo $user->profile->email;
有一点需要注意的是,关联方法的命名规范是驼峰法,而关联属性则一般是小写+下划线的方式,系统在获取的时候会自动转换对应,读取
user_profile
关联属性则对应的关联方法应该是userProfile
。
二、根据关联数据查询
// 查询用户昵称是think的用户
// 注意第一个参数是关联方法名(不是关联模型名)
$users = User::hasWhere('profile', ['nickname'=>'think'])->select();
// 可以使用闭包查询
$users = User::hasWhere('profile', function($query) {
$query->where('nickname', 'like', 'think%');
})->select();
三、预载入查询
with
方法可以传入数组,表示同时对多个关联模型(支持不同的关联类型)进行预载入查询。
$users = User::with(['profile','book'])->select();
foreach ($users as $user) {
echo $user->profile->name;
foreach($user->book as $book) {
echo $book->name;
}
}
四、关联保存
$user = User::get(1);
// 如果还没有关联数据 则进行新增
$user->profile()->save(['email' => 'thinkphp']);
定义相对关联
belongsTo('关联模型','外键','关联主键');
除了关联模型外,其它参数都是可选。
- 关联模型(必须):模型名或者模型类名
- 外键:当前模型外键,默认的外键名规则是关联模型名+
_id
- 关联主键:关联模型主键,一般会自动获取也可以指定传入
默认的关联外键是user_id
,如果不是,需要在第二个参数定义
<?php
namespace app\index\model;
use think\Model;
class Profile extends Model
{
public function user()
{
return $this->belongsTo('User','uid');
}
}
我们就可以根据档案资料来获取用户模型的信息
$profile = Profile::get(1);
// 输出User关联模型的属性
echo $profile->user->account;
一、绑定属性到父模型
可以在定义关联的时候使用bind
方法绑定属性到父模型,例如:
<?php
namespace app\index\model;
use think\Model;
class User extends Model
{
public function profile()
{
return $this->hasOne('Profile','uid')->bind('nickname,email');
}
}
或者使用数组的方式指定绑定属性别名
<?php
namespace app\index\model;
use think\Model;
class User extends Model
{
public function profile()
{
return $this->hasOne('Profile','uid')->bind([
'email',
'truename' => 'nickname',
'profile_id' => 'id',
]);
}
}
然后使用关联预载入查询的时候,可以使用
$user = User::get(1,'profile');
// 输出Profile关联模型的email属性
echo $user->email;
echo $user->profile_id;
绑定关联模型的属性支持读取器。
如果不是预载入查询,请使用模型的
appendRelationAttr
方法追加属性。
二、关联自动写入
我们可以使用together
方法更方便的进行关联自动写入操作。
写入
$blog = new Blog;
$blog->name = 'thinkphp';
$blog->title = 'ThinkPHP5关联实例';
$content = new Content;
$content->data = '实例内容';
$blog->content = $content;
$blog->together('content')->save();
如果绑定了子模型的属性到当前模型,可以用数组指定子模型的属性
$blog = new Blog;
$blog->name = 'thinkphp';
$blog->title = 'ThinkPHP5关联实例';
$blog->content = '实例内容';
// title和content是子模型的属性
$blog->together(['content'=>['title','content']])->save();
更新
// 查询
$blog = Blog::get(1);
$blog->title = '更改标题';
$blog->content->data = '更新内容';
// 更新当前模型及关联模型
$blog->together('content')->save();
删除
// 查询
$blog = Blog::get(1,'content');
// 删除当前及关联模型
$blog->together('content')->delete();
复制
如果不想这么麻烦每次调用
together
方法,也可以直接在模型类中定义relationWrite
属性,但必须是数组方式。不过考虑到模型的独立操作的可能性,并不建议。