一对一关联
版本 | 功能调整 |
---|---|
5.0.5 | 增加关联自动写入和删除 |
5.0.4 | 增加关联属性绑定到父模型功能 |
定义
定义一对一关联,例如,一个用户都有一个个人资料,我们定义User
模型如下:
namespace
app
index
model;
use
think
Model;
class
User
extends
Model
{
public
function
profile
()
{
return
$this->hasOne('Profile'); } }
hasOne
方法的参数包括:
hasOne('关联模型名','外键名','主键名',['模型别名定义'],'join类型');
默认的join
类型为INNER
。
V5.0.3+
版本开始,可以支持为关联模型定义需要查询的字段,例如:
namespace
app
index
model;
use
think
Model;
class
User
extends
Model
{
public
function
profile
()
{
return
$this->hasOne('Profile')->field('id,name,email'); } }
如果使用的是
join
方式的关联,不支持指定field字段。
5.0.5+
版本开始,模型别名定义参数已经废弃。
关联查找
定义好关联之后,就可以使用下面的方法获取关联数据:
$user = User::get(1);
// 输出Profile关联模型的email属性
echo
$user->profile->email;
如果要根据关联表的查询条件查询当前模型的数据,可以使用hasWhere
方法,例如:
$user = User::hasWhere('profile',['email'=>'[email protected]'])->find();
echo
$user->name;
默认情况下, 我们使用的是user_id
作为外键关联,如果不是的话则需要在关联定义的时候指定,例如:
<?php
namespace
app
index
model;
use
think
Model;
class
User
extends
Model
{
public
function
profile
()
{
return
$this->hasOne('Profile','uid'); } }
有一点需要注意的是,关联方法的命名规范是驼峰法,而关联属性则一般是小写+下划线的方式,系统在获取的时候会自动转换对应,读取
user_profile
关联属性则对应的关联方法应该是userProfile
。
关联新增
$user = User::get(1);
// 如果还没有关联数据 则进行新增
$user->profile()->save(['email'
=>
'thinkphp']);
系统会自动把当前模型的主键传入profile模型。
关联更新
和新增一样使用save
方法进行更新关联数据。
$user = User::get(1); $user->profile->email =
'thinkphp'; $user->profile->save();
// 或者
$user->profile->save(['email'
=>
'thinkphp']);
定义相对的关联
我们可以在Profile
模型中定义一个相对的关联关系,例如:
namespace
app
index
model;
use
think
Model;
class
Profile
extends
Model
{
public
function
user
()
{
return
$this->belongsTo('User'); } }
belongsTo
的参数包括:
belongsTo('关联模型名','外键名','关联表主键名',['模型别名定义'],'join类型');
默认的关联外键是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;
绑定属性到父模型(V5.0.4+
)
可以在定义关联的时候使用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方法追加属性。
关联自动写入(V5.0.5+
)
我们可以使用together
方法更方便的进行关联自动写入操作。
写入
$blog =
new
Blog; $blog->name =
'thinkphp'; $blog->title =
'ThinkPHP5关联实例'; $content =
new
Content; $content->data =
'实例内容'; $blog->content = $content; $blog->together('content')->save();
更新
// 查询
$blog = Blog::get(1); $blog->title =
'更改标题'; $blog->content->data =
'更新内容';
// 更新当前模型及关联模型
$blog->together('content')->save();
删除
// 查询
$blog = Blog::get(1);
// 删除当前及关联模型
$blog->together('content')->delete();