thinkphp 6.0 (一对多关联)

该文章展示了在ThinkPHP框架中如何处理一对多(hasMany)的模型关系,包括查询用户的文章、使用has()方法进行条件查询以及关联删除操作。文章提供了代码示例,解释了如何定义和使用这些关系,并附带了相关的数据库表结构。
摘要由CSDN通过智能技术生成

hasmany一对多 · thinkphp模型 · 看云 

<?php
declare (strict_types = 1);

namespace app\controller;

use think\Request;
use app\model\Article;
class Test
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
         // 查询用户及其文章
         $Articles = Article::with('posts')->select()->toArray();
         dd($Articles);
        
        //  使用has()方法,查询关联附表的主表内容,***比如大于等于2条的主表记录,这个也相当于反向查询了,从附表推出主表数据
        //  $Articles = Article::has('posts','>=',2)->select()->toArray();
        //  dd($Articles);


        // 关联删除
        // 在删除文章的同时删除下面的评论
        // $Articles = Article::with('posts')->find(2);
        // $Articles->together(['posts'])->delete();
        // dd($Articles);

    }

    
}
<?php
declare (strict_types = 1);
namespace app\model;
use think\Model;

/**
 * @mixin \think\Model
 */
class Article extends Model
{
     // 定义一对多关联
     public function posts()
     {
            // hasMany('关联模型','外键','主键');
            //关联模型(必须):关联模型类名
            //外键:关联模型外键,默认的外键名规则是当前模型名+_id,如:Comments表的article_id
            //主键:当前模型主键,一般会自动获取也可以指定传入,如:Article 表的id
         return $this->hasMany('Comments', 'article_id');
     }
}
<?php
declare (strict_types = 1);

namespace app\model;

use think\Model;

/**
 * @mixin \think\Model
 */
class Comments extends Model
{
    // 在model中隐藏不想显示的字段
    protected $hidden = ['article_id'];
     // 定义多对一关联
    //  public function user()
    //  {
            //https://www.choudalao.com/article/43
    //      return $this->belongsTo('Article');
    //  }

}

数据库表结构:

article表

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for article
-- ----------------------------
DROP TABLE IF EXISTS `article`;
CREATE TABLE `article`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `content` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = MyISAM AUTO_INCREMENT = 4 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of article
-- ----------------------------
INSERT INTO `article` VALUES (1, '文章1', '文章1内容');
INSERT INTO `article` VALUES (2, '文章2', '节省空间打开手机');
INSERT INTO `article` VALUES (3, '这是第三篇文章', '继续卷啊');

SET FOREIGN_KEY_CHECKS = 1;

 comments表:

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for comments
-- ----------------------------
DROP TABLE IF EXISTS `comments`;
CREATE TABLE `comments`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `article_id` int(11) NULL DEFAULT NULL,
  `content` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = MyISAM AUTO_INCREMENT = 5 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of comments
-- ----------------------------
INSERT INTO `comments` VALUES (1, 1, '评论:第一篇');
INSERT INTO `comments` VALUES (2, 1, '评论:第一篇的第二个评论');
INSERT INTO `comments` VALUES (3, 3, '评论:第三篇');
INSERT INTO `comments` VALUES (4, 2, '第二文章的评论');

SET FOREIGN_KEY_CHECKS = 1;

 运行结果:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值