https://blog.csdn.net/github_37512301/article/details/75675054

一、关联模型
在关系型数据库中,表之间有一对一、一对多、多对多的关系。在 TP5 中,实现了ORM (Object Relational Mapping) 的思想,通过在模型中建立模型间的关联,实现建立表与表之间的关联。

二、文章中用到的表结构

所用的数据表和数据传到了百度云

链接:http://pan.baidu.com/s/1hrXwEJa 密码:9r98

image 表,存储图片的位置信息

banner 推荐位表,存储推荐位的类型

banner_item 表,推荐位中的信息条目,可以看到它拥有外键 img_id

theme 表,商品活动主题,包含头图,主题图

product 表,商品表

theme_product 表, theme 与 product 的中间表

可以建立以下的 E-R图,一个 banner可以用有多个 banner_item,一个banner_iten 拥有一个 image;

theme 与 product 是多对多关系,

图1 表之间关系

三、从问题出发讲解关联

(1)查询 banner 并包含其下的 banner_item

由图1可知,我们要在 banner 与 banner_item 之间建立一对多的关联关系

class Banner extends Model
{
public function items() { //建立一对多关联
return $this->hasMany('BannerItem', 'banner_id', 'id'); //关联的模型,外键,当前模型的主键
} public static function getBannerByID($id)
{
$banner = self::with('items')->find($id); // 通过 with 使用关联模型,参数为关联关系的方法名
return $banner;
}
}

查询数据可得以下结果

{
"id": 1,
"name": "首页置顶",
"description": "首页轮播图",
"items": [
{
"id": 1,
"img_id": 65,
"key_word": "6",
"type": 1,
"banner_id": 1
},
{
"id": 2,
"img_id": 2,
"key_word": "25",
"type": 1,
"banner_id": 1
},
{
"id": 3,
"img_id": 3,
"key_word": "11",
"type": 1,
"banner_id": 1
},
{
"id": 5,
"img_id": 1,
"key_word": "10",
"type": 1,
"banner_id": 1
}
]
}

可以发现,在 items 下为一个数组,说明一个 banner 包含多个  banner_item ,有一个问题, items下面是 img_id,客户端需要图片路径,不需要 img_id,所以我们还需要建立 BannerItem 与 Image 模型间的关系。这时,Banner 与 BannerItem有一对多关联,BannerItem 与 Image 有一对一关联,这种关联在 TP5 中称为嵌套关联。继续完善代码。

BannerItem.php

class BannerItem extends Model
{
protected $hidden = ['delete_time', 'update_time'];
/**
* 建立与 Image 表的关联模型(一对一)
* @return \think\model\relation\BelongsTo
*/
public function img() {
return $this->belongsTo('Image', 'img_id', 'id'); //关联模型名,外键名,关联模型的主键
}
}

Banner.php

class Banner extends Model
{
public function items() {
return $this->hasMany('BannerItem', 'banner_id', 'id');
} public static function getBannerByID($id)
{
$banner = self::with(['items', 'items.img'])->find($id); // with 接收一个数组
return $banner;
}
}

这里 items.img 这种语法并不太好理解,我们可以根据语境解释,在一个 Banner 下需要包含多个 BannerItem,而每个 BannerItem 下面又对应一个 Image。
查询结果:

{
"id": 1,
"name": "首页置顶",
"description": "首页轮播图",
"items": [
{
"id": 1,
"img_id": 65,
"key_word": "6",
"type": 1,
"banner_id": 1,
"img": {
"url": "http://z.cn/images/banner-4a.png"
}
},
{
"id": 2,
"img_id": 2,
"key_word": "25",
"type": 1,
"banner_id": 1,
"img": {
"url": "http://z.cn/images/banner-2a.png"
}
},
{
"id": 3,
"img_id": 3,
"key_word": "11",
"type": 1,
"banner_id": 1,
"img": {
"url": "http://z.cn/images/banner-3a.png"
}
},
{
"id": 5,
"img_id": 1,
"key_word": "10",
"type": 1,
"banner_id": 1,
"img": {
"url": "http://z.cn/images/banner-1a.png"
}
}
]
}

这样的结果就可以被客户端处理了。

(2)hasOne 与 belongsTo 的区别

一对一关系,存在主从关系(主表和从表 ),主表不包含外键,从表包含外键。

hasOne 和 belongsTo 都是一对一关系,区别:
在主表的模型中建立关联关系,用 hasOne
在从表模型中建立关联关系,用 belongsTo
所以,我们在 BannerItem 中建立与 Image 的关系,用的是 belongsTo ,而不是 hasOne。相反,如果想在 Image 中查询到 BannerItem 的内容,需要用 hasOne 。

(3)查询 theme 并包含其下的 product
为了让查询的主题包含图片,所以我们要建立 theme 与 product 和 image 的关联关系,theme 中 topic_img_id 和 head_img_id 与 image 的 id 都是一对一的关系,theme 与 product 是多对多关联。

class Theme extends Model
{ /**
* 建立 theme 表中 topic_img_id 与 image 表 id 的一对一关系
* @return \think\model\relation\BelongsTo
*/
public function topicImg()
{
return $this->belongsTo('Image', 'topic_img_id', 'id');
} public function headImg()
{
return $this->belongsTo('Image', 'head_img_id', 'id');
} /**
* 建立多对多关联模型
* @return \think\model\relation\BelongsToMany
*/
public function products()
{
//关联模型名,中间表名,外键名,当前模型外键名
return $this->belongsToMany('Product', 'theme_product', 'product_id', 'theme_id');
}
/** * 返回 theme和poducts * @id theme id * @return theme数据模型 */
public static function getThemeWithProducts($id)
{
$theme = self::with('products,topicImg,headImg') ->find($id); return $theme;
}
}

查询结果为

[
{
"id": 1,
"name": "专题栏位一",
"description": "美味水果世界",
"topic_img_id": 16,
"delete_time": null,
"head_img_id": 49,
"update_time": "1970-01-01 08:00:00",
"topic_img": {
"url": "http://z.cn/images/1@theme.png"
},
"head_img": {
"url": "http://z.cn/images/1@theme-head.png"
}
},
{
"id": 2,
"name": "专题栏位二",
"description": "新品推荐",
"topic_img_id": 17,
"delete_time": null,
"head_img_id": 50,
"update_time": "1970-01-01 08:00:00",
"topic_img": {
"url": "http://z.cn/images/2@theme.png"
},
"head_img": {
"url": "http://z.cn/images/2@theme-head.png"
}
},
{
"id": 3,
"name": "专题栏位三",
"description": "做个干物女",
"topic_img_id": 18,
"delete_time": null,
"head_img_id": 18,
"update_time": "1970-01-01 08:00:00",
"topic_img": {
"url": "http://z.cn/images/3@theme.png"
},
"head_img": {
"url": "http://z.cn/images/3@theme.png"
}
}
]

可以看到,有的属性前端并不需要使用,比如 topic_img_id,delete_time等,所以还需要隐藏字段

在 Theme.php 中加入

protected $hidden = ['topic_img_id', 'head_img_id', 'delete_time', 'update_time'];

这里只是结合例子,讲了关联模型的简单使用。祝大家学习愉快。

实例讲解TP5中关联模型的更多相关文章

  1. 实例讲解JQuery中this和$(this)区别

    这篇文章主要介绍了实例讲解JQuery中this和$(this)的区别,this表示当前的上下文对象是一个html对象,可以调用html对象所拥有的属性和方法,$(this),代表的上下文对象是一个j ...

  2. <记录>TP5 关联模型使用(嵌套关联、动态排序以及隐藏字段)

    在数据库设计中,常常会有如下这种关联模型,分类表中一条分类对应多个商品表中的商品 如果要获得分类表中每条分类 以及 对应的商品的信息,则需要先查询分类表中的数据,然后根据结果遍历查询商品表,最后把数据 ...

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

    在数据库设计中,常常会有如下这种关联模型,分类表中一条分类对应多个商品表中的商品 如果要获得分类表中每条分类 以及 对应的商品的信息,则需要先查询分类表中的数据,然后根据结果遍历查询商品表,最后把数据 ...

  4. Java入门系列:实例讲解ArrayList用法

    本文通过实例讲解Java中如何使用ArrayList类. Java.util.ArrayList类是一个动态数组类型,也就是说,ArrayList对象既有数组的特征,也有链表的特征.可以随时从链表中添 ...

  5. MySQL Group By 实例讲解(二)

    mysql group by使用方法实例讲解 MySQL中GROUP BY语句用于对某个或某些字段查询分组,并返回这个字段重复记录的第一条,也就是每个小组(无排序)里面的第一条. 本文章通过实例向大家 ...

  6. ThinkPHP关联模型详解

    在ThinkPHP中,关联模型更类似一种mysql中的外键约束,但是外键约束更加安全,缺点却是在写sql语句的时候不方便,ThinkPHP很好得解决了这个问题.但是很多人不动关联模型的意思.现在就写个 ...

  7. tp5 模型中 关联查询(省去了foreach写法)

    1.控制器中 $list = Userlawsbook::where($where)->with('lawsbook')->paginate(7);  // 此处查出来为数组对象 dump ...

  8. TP5.1框架中的模型关联

    一对一关联 hasOne('关联模型','外键','主键'); 关联模型(必须):关联的模型名或者类名 外键:默认的外键规则是当前模型名(不含命名空间,下同)+_id ,例如user_id 主键:当前 ...

  9. ARIMA模型实例讲解——网络流量预测可以使用啊

    ARIMA模型实例讲解:时间序列预测需要多少历史数据? from:https://www.leiphone.com/news/201704/6zgOPEjmlvMpfvaB.html   雷锋网按:本 ...

随机推荐

  1. Android 集成ShareSDK分享QQ或空间成功后,回调却不执行的原因

    AndroidMainifest.xml中的如箭头所示的id一定要与assets下ShareSDK.xml中配置的QQ的AppId一定要相同. 如下图

  2. 电子书阅读(epub) --- calibre

    https://calibre-ebook.com/download   calibre https://www.fosshub.com/Calibre.html   win 64 bit calib ...

  3. 实现类似于QQ空间相册的点击图片放大,再点后缩小回原来位置

    前几天看到了有人在android5.0上实现了如下图一样的效果,我自己就去搜了下.参考了国外一篇文章和国内的一篇文章,最终实现了想要的效果.具体参考的网址我已经贴到文章末尾,大家可以去英文的那个网站看 ...

  4. git rebase 操作撤销

    git rebase可以更改提交历史,在不影响别人的情况下,能够重整git树. 但如果git rebase操作失误,却在push后才发现,怎么撤销rebase操作呢? 使用git reflog + g ...

  5. 8月10日CSS总结

    1.三角形光标转换 CSS: .one{ width: 0;­­ height: 0; border-top: 100px solid #000; border-right:100px solid t ...

  6. IDEA中将WEB-INF\lib下的Jar包添加到项目中

    打开Project Structure[可以使用快捷键:Ctrl+Alt+Shift+S]左侧选中Modules,在Dependecies中,点击右侧“+”号,选择JARS or directorie ...

  7. 用CSS画基本图形

    用CSS画基本图形 1.正方形 代码如下: #square { width: 100px; height: 100px; background: red; } 2.长方形 代码如下:   #recta ...

  8. SPOJ - TSUM 母函数+FFT+容斥

    题意:n个数,任取三个加起来,问每个可能的结果的方案数. 题解:构造母函数ABC,比如现在有 1 2 3 三个数.则 其中B表示同一个数加两次,C表示用三次.然后考虑去重. A^3表示可重复地拿三个. ...

  9. 变长编码表 ASCII代码等长编码

    小结: 1.ASCII编码.GBK编码不是变长编码: 2.数据压缩: 示例: aabacdab → 00100110111010 → |0|0|10|0|110|111|0|10| → aabacda ...

  10. JavaWeb开发如何用Tomcat部署发布

    一.如何安装TomCat 1.1安装包下载地址:https://tomcat.apache.org/download-70.cgi 1.2  安装exe文件,下一步直到安装成功.并启动Tomcat服务 ...