牛骨文教育服务平台(让学习变的简单)
博文笔记

实例讲解TP5中关联模型

创建时间:2017-07-21 投稿人: 浏览次数:133

一、关联模型

在关系型数据库中,表之间有一对一、一对多、多对多的关系。在 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 	hinkmodel
elationBelongsTo
     */
    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 都是一对一关系,区别:
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。