远程一对一关联用于定义有跨表的一对一关系,例如:
就可以直接通过远程一对一关联获取每个用户的档案卡,User
模型定义如下:
<?phpnamespace app\model;use think\Model;class User extends Model{ public function card() { return $this->hasOneThrough(Card::class,Profile::class); }}
远程一对一关联,需要同时存在Card
和Profile
模型。
hasOneThrough
方法的参数如下:
hasOneThrough('关联模型', '中间模型', '外键', '中间表关联键','当前模型主键','中间模型主键');
_id
_id
我们可以通过下面的方式获取关联数据
$user = User::find(1);// 获取用户的档案卡dump($user->card);