Eyoucms程序开发:一对一关联
版本 | 功能调整 |
---|---|
5.0.5 | 增加关联自动写入和删除 |
5.0.4 | 增加关联属性绑定到父模型功能 |
定义
定义一对一关联,例如,一个用户都有一个个人资料,我们定义User
模型如下:
namespace appindexmodel;
use thinkModel;
class User extends Model
{
public function profile()
{
return $this->hasOne('Profile');
}
}
hasOne
方法的参数包括:
hasOne('关联模型名','外键名','主键名',['模型别名定义'],'join类型');
默认的join
类型为INNER
。
V5.0.3+
版本开始,可以支持为关联模型定义需要查询的字段,例如:
namespace appindexmodel;
use thinkModel;
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'=>'thinkphp@qq.com'])->find();
echo $user->name;
默认情况下, 我们使用的是user_id
作为外键关联,如果不是的话则需要在关联定义的时候指定,例如:
namespace appindexmodel;
use thinkModel;
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 appindexmodel;
use thinkModel;
class Profile extends Model
{
public function user()
{
return $this->belongsTo('User');
}
}
belongsTo
的参数包括:
belongsTo('关联模型名','外键名','关联表主键名',['模型别名定义'],'join类型');
默认的关联外键是user_id
,如果不是,需要在第二个参数定义
namespace appindexmodel;
use thinkModel;
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方法绑定属性到父模型,例如:
namespace appindexmodel;
use thinkModel;
class User extends Model
{
public function profile()
{
return $this->hasOne('Profile','uid')->bind('nickname,email');
}
}
或者使用数组的方式指定绑定属性别名
namespace appindexmodel;
use thinkModel;
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();
相关文档
- 【冲顶神器功能介绍】实用功能盘点!助力高效操作,超省心
- 旺道SEO系统是什么?有啥用?核心功能大揭秘
- 手机网站排名优化软件到底是啥?有哪些重要功能?
- 企业网站管理系统挑选有门道?功能需求与操作简便性超关键
- 论坛短消息群发功能介绍,常见问题及操作步骤详解
- 淘宝新手上路指南!APP基本功能介绍及购物注意事项
- 想网上推广?刷论坛推广软件功能及靠谱选择方法来啦
- 免费Discuz论坛怎么用?有啥好处?功能全活儿超适合新手
- 免费论坛群发器好用吗?核心功能模块拆解及基本概念介绍
- 百中搜优化软件是啥?有啥用?功能模块大揭秘
- 企业门户:现代办公数字化平台,功能模块搭建要点有哪些?
- 凡科建站:网站建设领域有名堂,功能强大助你轻松建站
- 百度空间介绍:能创建个人网页,有写日志等多种功能
- 站优云:网络人关注的平台,其功能模块你了解多少?
- 关键词排名工具是啥?能帮上忙吗?查询核心功能模块了解下
- 天津SEO旺道是啥?能否助力网站优化?功能实效需考量
- 百度移动应用受众多手机用户青睐,功能亮点大揭秘
- 枫林SEO走红网络,这工具的关键词分析功能太绝了
- 快速排名工具怎么选怎么用?核心功能选择要点你知道吗?
- 谷歌站长平台工具:搜索效果报告 功能解读
上一篇: Eyoucms程序开发:输出替换
下一篇: Eyoucms程序开发:一对多关联