ThinkPHP5.0中的操作ORM的一对一,一对多,多对多的操作;

  由以下表举例:

banner表的设计

id name description delete_time update_time
1 首页置顶 首页轮播图    

banner_item表的设计

id img_id key_word type delete_time banner_id update_time
1 65 6 1   1  
2 2 25 1   1  
3 3 11 1   1  
5 1 10 1   1  

image表的设计

id url from delete_time update_time
1 /banner-1a.png 1    
2 /banner-2a.png 1    
3 /banner-3a.png 1    
4 /category-cake.png 1    

theme表的设计

id name description topic_img_id delete_time head_img_id update_time
1 专题栏位一 美味水果世界 16   49  
2 专题栏位二 新品推荐 17   50  
3 专题栏位三 做个干物女 18   18  

product表的设计

id name price stock delete_time category_id main_img_url from create_time update_time summary img_id
1 芹菜 半斤 0.01 998   3 /product-vg@1.png 1       13
2 梨花带雨 3个 0.01 989   2 /product-dryfruit@1.png 1       10
3 素米 327克 0.01 988   7 /product-rice@1.png 1       31
4 红袖枸杞 6克*3袋 0.01 987   6 /product-tea@1.png 1       32

theme_product表的设计

theme_id product_id
1 2
1 5
2 1
2 2
3 15

  1> 一对一的操作(belongsTo   hasOne)

    banner_item表与image表中图片表中的一对一关系(一个banner_item对应一张图片,一张图片只能对应一个banner_item图)

      ①.banner_item表中都有image表的外键img_id,在banner_item模型中写一下关联(belongsTo)

 <?php

 namespace app\api\model;

 class BannerItem extends BaseModel
 {
     public function img(){
         return $this->belongsTo('Image','img_id','id');
     }
 }

      ②.如果在image模型中写关联(hasOne)

 <?php

 namespace app\api\model;

 class Image extends BaseModel
 {
     protected $hidden = ['id','from','delete_time','update_time'];
     /**
      *图片获取器
      */
     public function getUrlAttr($value,$data){
         return $this->prefixImgUrl($value,$data);
     }

     /**
       */
     public function img(){
19         return $this->hasOne('BannerItem','img_id','id');
     }
 }

  2>一对多的操作(hasMany)

    banner表和banner_item表中的一对多关系(一个banner位可能对相应多个bannerItem,一个bannerItem只对应一个banner位)

      ①.banner_item表中存在banner表的外键banner_id,在banner模型中写一对多的关联模型(hasMany)

<?php
/**
 * Created by PhpStorm.
 * User: byzy
 * Date: 2020/1/19
 * Time: 10:53
 */

namespace app\api\model;

class Banner extends BaseModel
{
    protected $hidden = ['delete_time', 'update_time'];
    //关联
    public function items(){
        return $this->hasMany('BannerItem','banner_id','id');
    }
}





在banner控制器中使用关联模型查询:

 <?php

 namespace app\api\controller\v1;

 use app\api\model\Banner as BannerModel;

 class Banner
 {
     /***
      * 获取指定id的banner信息
      * @http GET
      * @id banner的id号
      */
     public function getBanner($id){
         $banner = BannerModel::with(['items','items.img'])->find($id);
         return json($banner);
     }
 }

注意:使用模型外关联用.连接 item.img






  3>多对多的的操作(belongsToMany)

    themeb表与product表之间存在多对多关系(一个theme主题对应多个商品,一个商品对应多个theme主题)

      由于两张表存在多对多的关系,所以可以通过构建第三张(theme_product)表来实现多对多的关系

      从Theme模型出发来关联出所有的关联关系:

 <?php

 namespace app\api\model;

 class Theme extends BaseModel
 {
     protected $hidden = ['delete_time','update_time','topic_img_id','head_img_id'];

     public function topicImg(){
         return $this->belongsTo('Image','topic_img_id','id');
     }

     public function headImg(){
         return $this->belongsTo('Image','head_img_id','id');
     }

     public function products(){
         return $this->belongsToMany('Product','theme_product','product_id','theme_id');
     }

     public static function getThemeWithProducts($id){
         $theme = self::with('topicImg,headImg,products')
             ->find($id);
         return $theme;
     }
 }

总结:ThinkPHP5.0中一对一两种(belongTo, hanOne)

          一对多一种(hasMany)

          多对多(belongsToMany)

ThinkPHP 5.0.7 + MySQL 构建RESTful API的小程序---02-ThinkPHP5中的orm的模型关联的更多相关文章

  1. 软件工程-构建之法 WordCount小程序 统计文件中字符串个数,单词个数,词频,行数

    一.前言 在之前写过一个词频统计的C语言课设,别人说你一个大三的怎么写C语言课程,我只想说我是先学习VB,VB是我编程语言的开始,然后接触到C语言及C++:再后来我是学习C++,然后反过来学习C语言, ...

  2. 使用ASP.NET Core 3.x 构建 RESTful API - 1.准备工作

    以前写过ASP.NET Core 2.x的REST API文章,今年再更新一下到3.0版本. 先决条件 我在B站有一个非常入门的ASP.NET Core 3.0的视频教程,如果您对ASP.NET Co ...

  3. Springboot 如何加密,以及利用Swagger2构建Restful API

    先看一下使用Swagger2构建Restful API效果图 超级简单的,只需要在pom 中引用如下jar包 <dependency> <groupId>io.springfo ...

  4. 用 Flask 来写个轻博客 (36) — 使用 Flask-RESTful 来构建 RESTful API 之五

    目录 目录 前文列表 PUT 请求 DELETE 请求 测试 对一条已经存在的 posts 记录进行 update 操作 删除一条记录 前文列表 用 Flask 来写个轻博客 (1) - 创建项目 用 ...

  5. 用 Flask 来写个轻博客 (33) — 使用 Flask-RESTful 来构建 RESTful API 之二

    Blog 项目源码:https://github.com/JmilkFan/JmilkFan-s-Blog 目录 目录 前文列表 扩展阅读 构建 RESTful Flask API 定义资源路由 格式 ...

  6. 使用ASP.NET Core构建RESTful API的技术指南

    译者荐语:利用周末的时间,本人拜读了长沙.NET技术社区翻译的技术标准<微软RESTFul API指南>,打算按照步骤写一个完整的教程,后来无意中看到了这篇文章,与我要写的主题有不少相似之 ...

  7. Spring MVC中使用 Swagger2 构建Restful API

    1.Spring MVC配置文件中的配置 [java] view plain copy <!-- 设置使用注解的类所在的jar包,只加载controller类 --> <contex ...

  8. 使用Express构建RESTful API

    RESTful服务 REST(Representational State Transfer)的意思是表征状态转移,它是一种基于HTTP协议的网络应用接口风格,充分利用HTTP的方法实现统一风格接口的 ...

  9. springboot集成swagger2构建RESTful API文档

    在开发过程中,有时候我们需要不停的测试接口,自测,或者交由测试测试接口,我们需要构建一个文档,都是单独写,太麻烦了,现在使用springboot集成swagger2来构建RESTful API文档,可 ...

随机推荐

  1. git push报错大文件,删除后重新commit依然报错

    git push报错: github不能上传大文件,按道理删掉重新提交就行了 可是删掉后,git add -A,再git commit,再git push,依然报错 后来我想明白了 github上传时 ...

  2. 【网摘】一个用shell写的俄罗斯方块

    一位大神的shell,值得学习:https://blog.csdn.net/zhenliang8/article/details/85316926 详见下: #!/bin/bash APP_NAME= ...

  3. HBase 分裂(split)

    1. 为什么split 最初一个Table 只有一个region(因此只能存放在一个region server上).随着数据的不断写入,HRegion越来越大,当到达一定程度后分裂为两个,通过负载均衡 ...

  4. javaweb项目部署到tomcat之后java文件没有编译

    1.选中你的项目==>选择Project 2.将Build Automatcally前的对号去掉后再Clean一下你的项目 这样就可以了,

  5. ELK学习实验004:Elasticsearch的简单介绍和操作

    一 集群节点 Elstaicsearch的集群是由多个节点组成都,通过cluster.name设置集权名称,比能切用与区分其他的集群,每个节点通过node.name指定节点 在Elasticsearc ...

  6. MyBatis原理-架构流程

    一 .MyBatis原理架构图 Mybatis的功能架构分为三层: API接口层:提供给外部使用的接口API,开发人员通过这些本地API来操纵数据库.接口层一接收到调用请求就会调用数据处理层来完成具体 ...

  7. 30.strftime参数

    附:strftime参数 strftime(format[, tuple]) -> string 将指定的struct_time(默认为当前时间),根据指定的格式化字符串输出 python中时间 ...

  8. Python3-Selenium自动化测试框架(二)之selenium使用和元素定位

    Selenium自动化测试框架(二)之selenium使用和元素定位 (一)selenium的简单使用 1.导包 from selenium import webdriver 2.初始化浏览器 # 驱 ...

  9. React Native的缓存和下载

    一般我们有3种数据需要缓存和下载:纯文本(比如API返回,状态标记等),图片缓存和其他静态文件. 纯文本 纯文本还是比较简单的,RN官方模块AsyncStorage就够了.它就跟HTML5里面的Loc ...

  10. CCPC-Wannafly Winter Camp Day1 (Div2 ABCFJ) 待补...

    Day1 Div2 场外链接 按题目顺序~ A 机器人 传送门 题意:有两条平行直线A.B,每条直线上有n个点,编号为1~n.在同一直线上,从a站点到b站点耗时为两点间的距离.存在m个特殊站点,只有在 ...