当前位置: 代码网 > it编程>编程语言>Php > ThinkPHP6 多模型关联查询操作记录

ThinkPHP6 多模型关联查询操作记录

2024年06月04日 Php 我要评论
新入职后组长安排了一个小的管理项目来检验能力,后发现自身对于 ThinkPHP 框架中的模型关联属于一窍不通,故被终止项目叫楼主去恶补 ThinkPHP6 框架知识。 对于多联表查询之前本人一直使用join方法,但是此方法对于代码效率和维护都有较大影响,故在此尝试使用 ThinkPHP 框架内置的模 ...

新入职后组长安排了一个小的管理项目来检验能力,后发现自身对于 thinkphp 框架中的模型关联属于一窍不通,故被终止项目叫楼主去恶补 thinkphp6 框架知识。

对于多联表查询之前本人一直使用join方法,但是此方法对于代码效率和维护都有较大影响,故在此尝试使用 thinkphp 框架内置的模型关联来对多联表进行操作(本次只记录查询操作)

一、建立数据表

首先我们先创建三个简易的数据表 user、article、comment 并添加一些测试数据,结构如下:
user:
image
article:
image
comment:
image
其中 article 表使用 user_id 与 user 表关联;comment 表使用 article_id 与 article 表进行关联。接下来我们来设置对应的数据库模型代码。

二、建立对应数据表模型,并添加关联

在 user 模型中,我们想实现根据提供的用户 id 查询出该用户对应的所有文章以及这些文章对应的评论

class user extends model
{
    //主键
    protected $pk = 'user_id';

    //关联文章
    public function articles(){
        return $this->hasmany(article::class, 'user_id', 'user_id');
    }
    //根据文章关联评论
    public function comments(){
        return $this->hasmanythrough(comment::class, article::class, 'user_id', 'article_id', 'user_id', 'article_id');
    }
}

由于 user 表与 comment 表没有直接的对应关系,所以我们使用 hasmanythrough() 方法来建立远程模型关联。其中 comment 是我们要关联的目标模型;article 是中间模型;第一个 user_id 是中间模型外键,第二个 article_id 是中间模型与目标模型关联键,第三个 user_id 是当前模型的主键,第四个 article_id 是中间模型的主键。

接下来我们来设置 article 模型的关联关系,要求实现根据提供的 id 查询出该文章与所对应的所有评论

class article extends model
{
    //主键
    protected $pk = 'article_id';

    //关联用户
    public function user()
    {
        return $this->belongsto(user::class, 'user_id', 'user_id');
    }

    //一对多关联评论
    public function comments()
    {
        return $this->hasmany(comment::class, 'article_id', 'article_id');
    }
}

最后我们还需要设置 comment 模型

class comment extends model
{
    //主键
    protected $pk = 'comment_id';

    //关联文章
    public function article()
    {
        return $this->belongsto(article::class, 'article_id', 'article_id');
    }
}

三、编写控制器关联查询代码

由于是演示代码,所以暂时都在 index 控制器下编写、同时我们默认所有的路由表已经配好

首先,我们来实现一个给出任意文章 id 我们返回此 id 的文章信息同时返回该文章的所有评论的功能

public function article($id)
{
    $article = article::with(['comments'])->find($id);

    return json_encode($article);
}

其中 with(['comments']) 对应我们在 article 模型中创建的关联方法,之后我们用 17 号文章请求这个接口,会得到如下数据:

{
  "article_id":17,
  "user_id":1,
  "title":"test2",
  "status":0,
  "comments":[
  {"comment_id":9,"article_id":17,"comment":"test comment 1","status":1},
  {"comment_id":10,"article_id":17,"comment":"test comment 2","status":1},
  {"comment_id":11,"article_id":17,"comment":"test comment 3","status":1}]
}

可以看到此时模型返回了 17 号文章信息与该文章下所有的评论信息。至此我们完成了一个一对多的双表关联查询

接下来我们尝试实现根据给出的用户 id 返回该用户对应的所有文章以及这些文章所对应的所有评论

public function user($id)
{
    $user = user::with(['articles' =>function($query){
        $query->with(['comments']);
    }])->find($id);

    return json_encode($user);
}

此时我们先使用 with(['articles']) 关联 article 表来查询文章,之后在 with() 方法内使用闭包来使 comment 表与 article 表进行关联,这样可以使查询到的评论与文章之间是父子结构;接下来我们使用 2 号用户请求这个接口,会得到如下数据:

{
  "user_id":2,
  "username":"test",
  "password":"123456",
  "articles":[
  	{"article_id":14,"user_id":2,"title":"test2","status":1,"comments":[]},
	{"article_id":15,"user_id":2,"title":"test3","status":1,"comments":[]},
	{"article_id":16,"user_id":2,"title":"test1","status":1,"comments":[
	  {"comment_id":6,"article_id":16,"comment":"test comment 1","status":1},
	  {"comment_id":7,"article_id":16,"comment":"test comment 2","status":1},
	  {"comment_id":8,"article_id":16,"comment":"test comment 3","status":1}]},
	{"article_id":19,"user_id":2,"title":"test1","status":1,"comments":[
	  {"comment_id":17,"article_id":19,"comment":"test comment 1","status":1},
	  {"comment_id":18,"article_id":19,"comment":"test comment 2","status":1},
	  {"comment_id":19,"article_id":19,"comment":"test comment 3","status":1}]},
	{"article_id":20,"user_id":2,"title":"test2","status":1,"comments":[
	  {"comment_id":20,"article_id":20,"comment":"test comment 1","status":1},
	  {"comment_id":21,"article_id":20,"comment":"test comment 2","status":1},
	  {"comment_id":22,"article_id":20,"comment":"test comment 3","status":1}]},
	{"article_id":21,"user_id":2,"title":"test3","status":1,"comments":[
	  {"comment_id":23,"article_id":21,"comment":"test comment 1","status":1},
	  {"comment_id":24,"article_id":21,"comment":"test comment 2","status":1},
	  {"comment_id":25,"article_id":21,"comment":"test comment 3","status":1}]}]
}

可以看到这样直接查询出了所有与用户对应的文章以及这些文章所对应评论,同时表与表之间的数据是有父子关系的。
而当我们不使用闭包函数,而是直接在 with() 方法内直接调用我们在 user 模型中定义的远程关联 comment 会返回什么样的数据呢?此时我们修改一下控制器中的代码

$user = user::with(['articles', 'comments'])->find($id);\\删除语句中的闭包

此时我们再次拿同样的 id 请求这个接口,会得到如下数据:

{
  "user_id":2,
  "username":"test",
  "password":"123456",
  "articles":[
  	{"article_id":14,"user_id":2,"title":"test2","status":1},
	{"article_id":15,"user_id":2,"title":"test3","status":1},
	{"article_id":16,"user_id":2,"title":"test1","status":1},
	{"article_id":19,"user_id":2,"title":"test1","status":1},
	{"article_id":20,"user_id":2,"title":"test2","status":1},
	{"article_id":21,"user_id":2,"title":"test3","status":1}],
  "comments":[
	{"comment_id":6,"article_id":16,"comment":"test comment 1","status":1},
	{"comment_id":7,"article_id":16,"comment":"test comment 2","status":1},
	{"comment_id":8,"article_id":16,"comment":"test comment 3","status":1},
	{"comment_id":17,"article_id":19,"comment":"test comment 1","status":1},
	{"comment_id":18,"article_id":19,"comment":"test comment 2","status":1},
	{"comment_id":19,"article_id":19,"comment":"test comment 3","status":1},
	{"comment_id":20,"article_id":20,"comment":"test comment 1","status":1},
	{"comment_id":21,"article_id":20,"comment":"test comment 2","status":1},
	{"comment_id":22,"article_id":20,"comment":"test comment 3","status":1},
	{"comment_id":23,"article_id":21,"comment":"test comment 1","status":1},
	{"comment_id":24,"article_id":21,"comment":"test comment 2","status":1},
	{"comment_id":25,"article_id":21,"comment":"test comment 3","status":1}]
}

可以看到虽然也都查询出了所有数据,但是文章与评论之间并不是父子关系而是同级关系。

至此双表与三表的多表之间模型关联查询的代码已经介绍演示完毕,具体的应用场景需要读者们根据自身的业务场景来具体处理;本文章仅代表个人的学习经验,具体代码与参数设置请参考thinkphp官方开发文档。

第一次写博客记录学习过程,如有错误还请大佬们多多指出。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com