tp5 模型关联

1 篇文章 0 订阅
1 篇文章 0 订阅

一、关联模型

在关系型数据库中,表之间有一对一、一对多、多对多的关系。在 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 之间建立一对多的关联关系

 

 
  1. class Banner extends Model

  2. {

  3. public function items() { //建立一对多关联

  4. return $this->hasMany('BannerItem', 'banner_id', 'id'); //关联的模型,外键,当前模型的主键

  5. }

  6.  
  7.  
  8. public static function getBannerByID($id)

  9. {

  10. $banner = self::with('items')->find($id); // 通过 with 使用关联模型,参数为关联关系的方法名

  11. return $banner;

  12. }

  13. }




查询数据可得以下结果

 

 

 
  1. {

  2. "id": 1,

  3. "name": "首页置顶",

  4. "description": "首页轮播图",

  5. "items": [

  6. {

  7. "id": 1,

  8. "img_id": 65,

  9. "key_word": "6",

  10. "type": 1,

  11. "banner_id": 1

  12. },

  13. {

  14. "id": 2,

  15. "img_id": 2,

  16. "key_word": "25",

  17. "type": 1,

  18. "banner_id": 1

  19. },

  20. {

  21. "id": 3,

  22. "img_id": 3,

  23. "key_word": "11",

  24. "type": 1,

  25. "banner_id": 1

  26. },

  27. {

  28. "id": 5,

  29. "img_id": 1,

  30. "key_word": "10",

  31. "type": 1,

  32. "banner_id": 1

  33. }

  34. ]

  35. }



 

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

BannerItem.php

 

 
  1. class BannerItem extends Model

  2. {

  3. protected $hidden = ['delete_time', 'update_time'];

  4. /**

  5. * 建立与 Image 表的关联模型(一对一)

  6. * @return \think\model\relation\BelongsTo

  7. */

  8. public function img() {

  9. return $this->belongsTo('Image', 'img_id', 'id'); //关联模型名,外键名,关联模型的主键

  10. }

  11. }




Banner.php

 

 

 
  1. class Banner extends Model

  2. {

  3. public function items() {

  4. return $this->hasMany('BannerItem', 'banner_id', 'id');

  5. }

  6.  
  7. public static function getBannerByID($id)

  8. {

  9. $banner = self::with(['items', 'items.img'])->find($id); // with 接收一个数组

  10. return $banner;

  11. }

  12. }



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

 

 

 
  1. {

  2. "id": 1,

  3. "name": "首页置顶",

  4. "description": "首页轮播图",

  5. "items": [

  6. {

  7. "id": 1,

  8. "img_id": 65,

  9. "key_word": "6",

  10. "type": 1,

  11. "banner_id": 1,

  12. "img": {

  13. "url": "http://z.cn/images/banner-4a.png"

  14. }

  15. },

  16. {

  17. "id": 2,

  18. "img_id": 2,

  19. "key_word": "25",

  20. "type": 1,

  21. "banner_id": 1,

  22. "img": {

  23. "url": "http://z.cn/images/banner-2a.png"

  24. }

  25. },

  26. {

  27. "id": 3,

  28. "img_id": 3,

  29. "key_word": "11",

  30. "type": 1,

  31. "banner_id": 1,

  32. "img": {

  33. "url": "http://z.cn/images/banner-3a.png"

  34. }

  35. },

  36. {

  37. "id": 5,

  38. "img_id": 1,

  39. "key_word": "10",

  40. "type": 1,

  41. "banner_id": 1,

  42. "img": {

  43. "url": "http://z.cn/images/banner-1a.png"

  44. }

  45. }

  46. ]

  47. }



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

 


(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 是多对多关联。

 
  1. class Theme extends Model

  2. {

  3.  
  4. /**

  5. * 建立 theme 表中 topic_img_id 与 image 表 id 的一对一关系

  6. * @return \think\model\relation\BelongsTo

  7. */

  8. public function topicImg()

  9. {

  10. return $this->belongsTo('Image', 'topic_img_id', 'id');

  11. }

  12.  
  13. public function headImg()

  14. {

  15. return $this->belongsTo('Image', 'head_img_id', 'id');

  16. }

  17.  
  18. /**

  19. * 建立多对多关联模型

  20. * @return \think\model\relation\BelongsToMany

  21. */

  22. public function products()

  23. {

  24. //关联模型名,中间表名,外键名,当前模型外键名

  25. return $this->belongsToMany('Product', 'theme_product', 'product_id', 'theme_id');

  26. }

  27. /** * 返回 theme和poducts * @id theme id * @return theme数据模型 */

  28. public static function getThemeWithProducts($id)

  29. {

  30. $theme = self::with('products,topicImg,headImg') ->find($id); return $theme;

  31. }

  32. }


 


 

 

查询结果为

 

 
  1. [

  2. {

  3. "id": 1,

  4. "name": "专题栏位一",

  5. "description": "美味水果世界",

  6. "topic_img_id": 16,

  7. "delete_time": null,

  8. "head_img_id": 49,

  9. "update_time": "1970-01-01 08:00:00",

  10. "topic_img": {

  11. "url": "http://z.cn/images/1@theme.png"

  12. },

  13. "head_img": {

  14. "url": "http://z.cn/images/1@theme-head.png"

  15. }

  16. },

  17. {

  18. "id": 2,

  19. "name": "专题栏位二",

  20. "description": "新品推荐",

  21. "topic_img_id": 17,

  22. "delete_time": null,

  23. "head_img_id": 50,

  24. "update_time": "1970-01-01 08:00:00",

  25. "topic_img": {

  26. "url": "http://z.cn/images/2@theme.png"

  27. },

  28. "head_img": {

  29. "url": "http://z.cn/images/2@theme-head.png"

  30. }

  31. },

  32. {

  33. "id": 3,

  34. "name": "专题栏位三",

  35. "description": "做个干物女",

  36. "topic_img_id": 18,

  37. "delete_time": null,

  38. "head_img_id": 18,

  39. "update_time": "1970-01-01 08:00:00",

  40. "topic_img": {

  41. "url": "http://z.cn/images/3@theme.png"

  42. },

  43. "head_img": {

  44. "url": "http://z.cn/images/3@theme.png"

  45. }

  46. }

  47. ]




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

 

在 Theme.php 中加入

 

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


 

 

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值