通知短信+运营短信,5秒速达,支持群发助手一键发送🚀高效触达和通知客户 广告
## 一对一关联 |版本|功能调整| |---|---| |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`方法的参数包括: >[info] ### 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'); } } ~~~ >[danger] 如果使用的是`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` 作为外键关联,如果不是的话则需要在关联定义的时候指定,例如: ~~~ <?php namespace app\index\model; use think\Model; class User extends Model { public function profile() { return $this->hasOne('Profile','uid'); } } ~~~ >[danger] 有一点需要注意的是,关联方法的命名规范是驼峰法,而关联属性则一般是小写+下划线的方式,系统在获取的时候会自动转换对应,读取`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`的参数包括: >[info] ### 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(); ~~~ 更新 ~~~php // 查询 $blog = Blog::get(1); $blog->title = '更改标题'; $blog->content->data = '更新内容'; // 更新当前模型及关联模型 $blog->together('content')->save(); ~~~ 删除 ~~~ // 查询 $blog = Blog::get(1); // 删除当前及关联模型 $blog->together('content')->delete(); ~~~ > 如果不想这么麻烦每次调用`together`方法,也可以直接在模型类中定义`relationWrite`属性,但必须是数组方式。不过考虑到模型的独立操作的可能性,并不建议。