1.表结构
2.模型实现
profile模型
<?php
namespace app\zjd\Model;
use think\Model;
class Profile extends Model
{
public function user()
{
return $this->belongsTo('User','user_id');
}
}
user模型
//模型一对一关联
public function City()
{
return $this->hasOne('City','id','cityId');
// return $this->hasOne('City','id','cityId')->field('name');
//等价
// return $this->belongsTo('city','cityId','id');
}
public function profile()
{
return $this->hasOne('Profile');
}
usercontroller
//模型关联
$user = User::get(2);
echo $req->profile;
$req = $user->city;
//关联修改
echo $req->city->save(
['name' =>'濮阳']
);