ThinkPHP 8的多对多关联

【图书介绍】《ThinkPHP 8高效构建Web应用》-CSDN博客

《2025新书 ThinkPHP 8高效构建Web应用 编程与应用开发丛书 夏磊 清华大学出版社教材书籍 9787302678236 ThinkPHP 8高效构建Web应用》【摘要 书评 试读】- 京东图书

使用VS Code开发ThinkPHP项目-CSDN博客

编程与应用开发_夏天又到了的博客-CSDN博客

多对多关联属于比较复杂的关联,需要借助一个中间表实现,在模型中使用belongsToMany定义。在介绍ThinkPHP 8的多对多语法之间,我们先来看一个例子,以加深对多对多关联的理解。

比如我们开发一个博客系统,每篇文章可以关联多个标签,每个标签可以关联多篇文章,涉及的数据表如表8-1~表8-3所示。

如果我们需要查询ThinkPHP教程这篇文章关联了哪些标签,可以用文章ID从文章标签关联表获得标签ID列表[1,2],再从标签表查询[1,2]的标签得到PHP和ThinkPHP。

查询PHP这个标签关联了哪些文章也是类似的,先用标签ID从文章标签关联表获得文章ID列表[1,2],再从文章表查询到两篇文章。

下面是文章标签多对多关联的ThinkPHP 8模型示例。首先根据上面3个表格创建数据表,SQL语句如下:

复制代码
CREATE TABLE `article` (
  `aid` int NOT NULL AUTO_INCREMENT,
  `title` varchar(45) NOT NULL,
  `content` varchar(45) NOT NULL,
  PRIMARY KEY (`aid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
CREATE TABLE `tag` (
  `tid` int NOT NULL AUTO_INCREMENT,
  `tname` varchar(45) COLLATE utf8mb3_unicode_ci NOT NULL,
  PRIMARY KEY (`tid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci;
CREATE TABLE `articletag` (
  `aid` int NOT NULL,
  `tid` int NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci;

再使用MySQL Workbench工具,按表8-1~表8-3给出的数据手工填充数据表。接下来就可以编写多对多关联示例代码了。

1. 文章表

文章表示例如下:

复制代码
<?php
namespace app\model;

use think\Model;

class ArticleModel extends Model 
{
    protected $pk = 'aid'; // 一定要声明主键  
    protected $table = 'article';
    // 设置字段信息
    protected $schema = [
        'aid'			=> 'int',
        'title'		=> 'string',
        'content'	=> 'string',
    ];
    
    public function tags()
    {
        return $this->belongsToMany(TagModel::class, ArticleTagModel::class ,foreignKey:'aid',localKey:'aid');
    }
}
2. 标签表

标签表示例如下:

复制代码
<?php
namespace app\model;

use think\Model;

class TagModel extends Model 
{
    protected $pk = 'tid'; // 一定要声明主键 
    protected $table = 'tag';
    // 设置字段信息
    protected $schema = [
        'tid'			=> 'int',
        'tname'	=> 'string',
    ];
    
    public function articles()
    {
        return $this->belongsToMany(ArticleModel::class, ArticleTagModel::class,foreignKey:'tid',localKey:'tid' );
    }
}
3. 文章标签关联表

需要注意的是,中间表模型需要继承think\model\Pivot,而不是使用默认的think\Model,示例如下:

复制代码
<?php
namespace app\model;
//中间表模型需要继承think\model\Pivot
use think\model\Pivot;

class ArticleTagModel extends Pivot 
{    
    protected $table = 'articletag';
    // 设置字段信息
    protected $schema = [
        'aid'	=> 'int',
        'tid'	=> 'int',
    ];
}
4. 关联查询

关联查询示例如下:

复制代码
use think\Model;
use app\model\ArticleModel;
use app\model\TagModel;
 
class Article 
{
    public function many2many()
    {
        $article = ArticleModel::with(['tags'])->find(1);
        //$article = ArticleModel::with(['tags'])->select();
        //print_r(  $article);
        //print_r(  $article->tags  );

        foreach($article->tags as $tag) {
            echo $tag->tname, PHP_EOL;
        }
    }
}

上面3个模型1个控制器完成后,运行服务器,在浏览器中访问http://localthost:8000/article/ many2many,可以关联查询出aid为1的文章,以及其标签有哪些。

相关推荐
Q_Q51100828519 小时前
python+nodejs+springboot在线车辆租赁信息管理信息可视化系统
spring boot·python·信息可视化·django·flask·node.js·php
_Re.19 小时前
DSC 归档配置相关
数据库·oracle·php
BingoGo1 天前
PHP 快速集成 ChatGPT 用 AI 让你的应用更聪明
后端·php
JaguarJack1 天前
PHP 快速集成 ChatGPT 用 AI 让你的应用更聪明
后端·php
Q_Q5110082851 天前
python+django/flask哈利波特书影音互动科普网站
spring boot·python·django·flask·node.js·php
qq_10055170752 天前
WordPress给指定分类文章添加一个自动化高亮(一键复制)功能
运维·前端·自动化·php
YC运维2 天前
LNMP架构(分离部署)PHP与数据库交互示例
数据库·架构·php
Q_Q5110082852 天前
python+django/flask+springboot实践性教学系统 实训任务发布 学生作业提交 教师评阅管理系统
spring boot·python·django·flask·node.js·php
喝奶茶的Blair2 天前
PHP应用&文件操作安全&上传下载&任意读取删除&目录遍历&文件包含(2024小迪安全Day32笔记)
笔记·安全·web安全·php
qq_252924192 天前
PHP 8.0+ 高级特性深度探索:架构设计与性能优化
android·性能优化·php