如何实现ThinkPHP6.x的复杂关联查询功能?

更新于
2026-09-17 16:52:00
49阅读来源:SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1294个文字,预计阅读时间需要6分钟。

如何实现ThinkPHP6.x的复杂关联查询功能?

一、一对一直联模式(hasOne)

适用于主表关联附表的情况,具体设置方式如下:

phphasOne('关联模型类名', '外键字段名', '主键字段名');

例如:

phpreturn $this->hasOne('Profile', 'user_id', 'id');

其中:

- 关联模型类名:指附表的模型类名。- 外键字段名:指主表中对应附表的关联字段名。- 主键字段名:指附表的主键字段名。

二、关联模型(关联模型)

关联模型是hasOne模式必须的,它定义了主表与附表之间的关联关系。以下是关联模型的示例:

phpclass Profile extends Model{ // 一对一直联 public function user() { return $this->hasOne('User', 'id', 'user_id'); }}

其中:

- `hasOne` 方法用于定义一对一直联关系。- `User` 是主表的模型类名。- `id` 是主表的主键字段名。- `user_id` 是附表中对应主表主键的外键字段名。

一.一对一关联

如何实现ThinkPHP6.x的复杂关联查询功能?

hasOne 模式

hasOne 模式,适合主表关联附表,具体设置方式如下:

hasOne('关联模型',['外键','主键']);

return $this->hasOne(Profile::class,'user_id', 'id');

关联模型(必须):关联的模型名或者类名

外键:默认的外键规则是当前模型名(不含命名空间,下同)+_id ,例如 user_id

主键:当前模型主键,默认会自动获取也可以指定传入

我们了解了表与表关联后,实现的查询方案;

$user = UserModel::find(21);

return $user->profile->hobby;

使用 save()方法,可以设置关联修改,通过主表修改附表字段的值;

$user = UserModel::find(19);

$user->profile->save(['hobby'=>'酷爱小姐姐']);

->profile 属性方式可以修改数据,->profile()方法方式可以新增数据;

$user->profile()->save(['hobby'=>'不喜欢吃青椒']);

belongsTo 模式

belongsTo 模式,适合附表关联主表,具体设置方式如下:

belongsTo('关联模型',['外键','关联主键']);

return $this->belongsTo(Profile::class,'user_id', 'id');

关联模型(必须):模型名或者模型类名

外键:当前模型外键,默认的外键名规则是关联模型名+_id

关联主键:关联模型主键,一般会自动获取也可以指定传入

对于 belongsTo()的查询方案,已经了解过,如下:

$profile = ProfileModel::find(1);

return $profile->user->email;

使用 hasOne()也能模拟 belongsTo()来进行查询;

//参数一表示的是 User 模型类的 profile 方法,而非 Profile 模型类

$user = UserModel::hasWhere('profile', ['id'=>2])->find();

return json($user);

//采用闭包,这里是两张表操作,会导致 id 识别模糊,需要指明表

$user = UserModel::hasWhere('profile', function ($query) {

$query->where('profile.id', 2);

})->select();

return json($user);

二.一对多关联查询

hasMany 模式

hasMany 模式,适合主表关联附表,实现一对多查询,具体设置方式如下:

hasMany('关联模型',['外键','主键']);

return $this->hasMany(Profile::class,'user_id', 'id');

关联模型(必须):模型名或者模型类名

外键:关联模型外键,默认的外键名规则是当前模型名+_id

主键:当前模型主键,一般会自动获取也可以指定传入

我们了解了表与表关联后,实现的查询方案;

$user = UserModel::find(19);

return json($user->profile);

使用->profile()方法模式,可以进一步进行数据的筛选;

$user->profile()->where('id', '>', 10)->select();

$user->profile->where('id', '>', 10)

使用 has()方法,查询关联附表的主表内容,比如大于等于 2 条的主表记录;

UserModel::has('profile', '>=', 2)->select();

使用 hasWhere()方法,查询关联附表筛选后记录,比如兴趣审核通过的主表记录;

UserModel::hasWhere('profile', ['status'=>1])->select();

使用 save()和 saveAll()进行关联新增和批量关联新增,方法如下:

$user = UserModel::find(19);

$user->profile()->save(['hobby'=>'测试喜好', 'status'=>1]);

$user->profile()->saveAll([

['hobby'=>'测试喜好', 'status'=>1],

['hobby'=>'测试喜好', 'status'=>1]

]);

使用 together()方法,可以删除主表内容时,将附表关联的内容全部删除;

$user = UserModel::with('profile')->find(227);

$user->together(['profile'])->delete();

本文共计1294个文字,预计阅读时间需要6分钟。

如何实现ThinkPHP6.x的复杂关联查询功能?

一、一对一直联模式(hasOne)

适用于主表关联附表的情况,具体设置方式如下:

phphasOne('关联模型类名', '外键字段名', '主键字段名');

例如:

phpreturn $this->hasOne('Profile', 'user_id', 'id');

其中:

- 关联模型类名:指附表的模型类名。- 外键字段名:指主表中对应附表的关联字段名。- 主键字段名:指附表的主键字段名。

二、关联模型(关联模型)

关联模型是hasOne模式必须的,它定义了主表与附表之间的关联关系。以下是关联模型的示例:

phpclass Profile extends Model{ // 一对一直联 public function user() { return $this->hasOne('User', 'id', 'user_id'); }}

其中:

- `hasOne` 方法用于定义一对一直联关系。- `User` 是主表的模型类名。- `id` 是主表的主键字段名。- `user_id` 是附表中对应主表主键的外键字段名。

一.一对一关联

如何实现ThinkPHP6.x的复杂关联查询功能?

hasOne 模式

hasOne 模式,适合主表关联附表,具体设置方式如下:

hasOne('关联模型',['外键','主键']);

return $this->hasOne(Profile::class,'user_id', 'id');

关联模型(必须):关联的模型名或者类名

外键:默认的外键规则是当前模型名(不含命名空间,下同)+_id ,例如 user_id

主键:当前模型主键,默认会自动获取也可以指定传入

我们了解了表与表关联后,实现的查询方案;

$user = UserModel::find(21);

return $user->profile->hobby;

使用 save()方法,可以设置关联修改,通过主表修改附表字段的值;

$user = UserModel::find(19);

$user->profile->save(['hobby'=>'酷爱小姐姐']);

->profile 属性方式可以修改数据,->profile()方法方式可以新增数据;

$user->profile()->save(['hobby'=>'不喜欢吃青椒']);

belongsTo 模式

belongsTo 模式,适合附表关联主表,具体设置方式如下:

belongsTo('关联模型',['外键','关联主键']);

return $this->belongsTo(Profile::class,'user_id', 'id');

关联模型(必须):模型名或者模型类名

外键:当前模型外键,默认的外键名规则是关联模型名+_id

关联主键:关联模型主键,一般会自动获取也可以指定传入

对于 belongsTo()的查询方案,已经了解过,如下:

$profile = ProfileModel::find(1);

return $profile->user->email;

使用 hasOne()也能模拟 belongsTo()来进行查询;

//参数一表示的是 User 模型类的 profile 方法,而非 Profile 模型类

$user = UserModel::hasWhere('profile', ['id'=>2])->find();

return json($user);

//采用闭包,这里是两张表操作,会导致 id 识别模糊,需要指明表

$user = UserModel::hasWhere('profile', function ($query) {

$query->where('profile.id', 2);

})->select();

return json($user);

二.一对多关联查询

hasMany 模式

hasMany 模式,适合主表关联附表,实现一对多查询,具体设置方式如下:

hasMany('关联模型',['外键','主键']);

return $this->hasMany(Profile::class,'user_id', 'id');

关联模型(必须):模型名或者模型类名

外键:关联模型外键,默认的外键名规则是当前模型名+_id

主键:当前模型主键,一般会自动获取也可以指定传入

我们了解了表与表关联后,实现的查询方案;

$user = UserModel::find(19);

return json($user->profile);

使用->profile()方法模式,可以进一步进行数据的筛选;

$user->profile()->where('id', '>', 10)->select();

$user->profile->where('id', '>', 10)

使用 has()方法,查询关联附表的主表内容,比如大于等于 2 条的主表记录;

UserModel::has('profile', '>=', 2)->select();

使用 hasWhere()方法,查询关联附表筛选后记录,比如兴趣审核通过的主表记录;

UserModel::hasWhere('profile', ['status'=>1])->select();

使用 save()和 saveAll()进行关联新增和批量关联新增,方法如下:

$user = UserModel::find(19);

$user->profile()->save(['hobby'=>'测试喜好', 'status'=>1]);

$user->profile()->saveAll([

['hobby'=>'测试喜好', 'status'=>1],

['hobby'=>'测试喜好', 'status'=>1]

]);

使用 together()方法,可以删除主表内容时,将附表关联的内容全部删除;

$user = UserModel::with('profile')->find(227);

$user->together(['profile'])->delete();