TP5 关联模型使用(嵌套关联、动态排序以及隐藏字段)

TP5 关联模型使用(嵌套关联、动态排序以及隐藏字段)

在数据库设计中,常常会有如下这种关联模型,分类表中一条分类对应多个商品表中的商品

TP5 关联模型使用(嵌套关联、动态排序以及隐藏字段)

如果要获得分类表中每条分类 以及 对应的商品的信息,则需要先查询分类表中的数据,然后根据结果遍历查询商品表,最后把数据拼接在一起

TP5中关联模型可以解决这一问题

普通关联

先创建分类表模型 /application/common/model/Category.php   以及商品表模型 /application/common/model/Goods.php

在分类表中创建关联

namespace app\common\model; class Category extends Base { public function goods(){ return $this->hasMany('Goods','category_id','id'); } }

 

 接着就可以使用关联模型查询数据

    public function list(){ return CategoryModel::with('goods')->where(true)->select(); }

 

 嵌套关联

TP5 关联模型使用(嵌套关联、动态排序以及隐藏字段)

 

 /application/common/model/Category.php

class Category extends Model { public function product(){ return $this->hasMany('product','category_id','id'); } }

 

 /application/common/model/Goods.php

class Product extends Model { public function property(){ return $this->hasMany('property','goods_id','id'); } }

 

在控制器中调用:

    public function index() { return Category::with('product,product.property')->where('id',1)->find(); }

 

在调用关联模型查询数据时,如果我们需要动态隐藏字段,或者给记录排序时可以这么做

class Category extends Model { public function product(){ return $this->hasMany('product','category_id','id'); } public function list(){ //在with中可以传递一个闭包函数,函数的参数为当前key锁对应模型的查询器 $this //在闭包函数中无需使用select或者find等返回数据 //如下操作返回 category中所有值,以及对应 product ,并且product按照price排序 return self::with([ 'product'=>function($query){ $query->with('property')->field('name')->order('price'); } ])->select(); } }

 

 

建立原则

1. 哪张表中建立外键那么那张表就是从表  

2. 理论上可以在关联的两张表中建立关联关系,例如用户表User 和用户信息表 Profile 是一对一的关系,假设在Profile表中user_id字段指向User表的id字段,那么在User表中可以建立外键

public function profile(){ return $this->hasOne('profile','user_id','id'); }

 

也可以在Profile表中建立

public function user(){ return $this->belongsTo('user','user_id','id'); }

 

建立原则:在哪张表中调用就在哪张表中建立,例如,通常情况下我们是希望通过查找用户user的同时也输出用户信息,所以我们查找的是user表,所有就在user表中建立关联

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

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/112470.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • patch文件的执行和制作

    patch文件的执行和制作执行 patch 文件 br patch p NUM0 1 4 patchbr nbsp br NUM setting p0givestheen p1givesbr nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp u howard src blurfl blurfl cbr nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp withoutthele p4givesbr nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp blurfl blurfl c 制作 p

    2025年11月15日
    4
  • Springboot自定义注解,支持SPEL表达式

    Springboot自定义注解,支持SPEL表达式举例,自定义redis模糊删除注解1.自定义注解importjava.lang.annotation.ElementType;importjava.lang.annotation.Retention;importjava.lang.annotation.RetentionPolicy;importjava.lang.annotation.Target;@Target(E…

    2025年6月17日
    7
  • MySql数据库约束

    关系型数据库系统和文件系统的一个不同点是,关系数据库本身能保证存储数据的完整性,不需要应用程序的控制,而文件系统一般需要在程序端进行控制。当前几乎所有的关系型数据库都提供了约束(constraits)

    2021年12月28日
    44
  • atop用法_atop linux 命令 在线中文手册

    atop用法_atop linux 命令 在线中文手册atop 简介本文要介绍的 atop 就是一款用于监控 Linux 系统资源与进程的工具 它以一定的频率记录系统的运行状态 所采集的数据包含系统资源 CPU 内存 磁盘和网络 使用情况和进程运行情况 并能以日志文件的方式保存在磁盘中 服务器出现问题后 我们可获取相应的 atop 日志文件进行分析 atop 是一款开源软件 我们可以从这里获得其源码和 rpm 安装包 一 atop 使用方法在安装 atop 之后 我们在命令行

    2025年11月4日
    7
  • LaTeX 换行、换页、空白空间[通俗易懂]

    LaTeX 换行、换页、空白空间[通俗易懂]一般来说,我们不推荐你改变默认的LaTeX文档结构。当然,我们有时候也有这个需求。所以,在本文中,我们将解释如何在文档中插入空行,以及插入任意的空白。

    2022年5月14日
    151
  • 移位寄存器实现序列检测-Verilog「建议收藏」

    移位寄存器实现序列检测-Verilog「建议收藏」//移位寄存器实现10010检测moduleDetect_10010( inputclk, inputrst_n, inputdata_in, outputreg[4:0]data_out, outputflag);always@(posedgeclkornegedgerst_n)begin if(!rst_n) data_out<=5’d0; else data_out<=({data_out[3:0],data_in

    2022年7月16日
    13

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号