ThinkPHP 3.2.3 关联模型的使用
关于关联模型
ThinkPHP 3.2.3 的关联模型(手册地址)一般处理关联数据表的 CURD 操作,例如关联读取、关联写入、关联删除等。
实例
博客管理模块关于博客有 4 张数据表:博客表 crm_blog:
CREATE TABLE `crm_blog` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT,`title` varchar(30) NOT NULL DEFAULT '',`content` text NOT NULL,`time` int(10) unsigned NOT NULL DEFAULT '0',`click` smallint(6) unsigned NOT NULL DEFAULT '0',`cid` int(10) unsigned NOT NULL,`del` tinyint(1) unsigned NOT NULL DEFAULT '0',PRIMARY KEY (`id`),KEY `cid` (`cid`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
属性表 crm_attr:
CREATE TABLE `crm_attr` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT,`name` char(10) NOT NULL DEFAULT '',`color` char(10) NOT NULL DEFAULT '',PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
博客-属性关联表 crm_blog_attr:
CREATE TABLE `crm_blog_attr` (`bid` int(10) unsigned NOT NULL,`aid` int(10) unsigned NOT NULL,KEY `bid` (`bid`),KEY `aid` (`aid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8
博客和属性是多对多的关系(MANY_TO_MANY)
类别表 crm_cate:
CREATE TABLE `crm_cate` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT,`name` char(20) NOT NULL DEFAULT '',`pid` int(10) unsigned NOT NULL DEFAULT '0',`sort` smallint(6) NOT NULL DEFAULT '100',PRIMARY KEY (`id`),KEY `pid` (`pid`) ) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8
博客相对于类别是一对多(BELONGS_TO)关系,另一个角度类别相对于博客是多对一(HAS_MANY)关系
在 Admin 应用下创建 Model 类:./Application/Admin/Model/BlogRelationModel.class.php:
1 <?php 2 namespace Admin\Model; 3 use Think\Model\RelationModel; 4 5 class BlogRelationModel extends RelationModel { 6 //如果Model文件名是BlogModel.class.php 就不需要定义$tableName 7 protected $tableName = 'blog';//以blog表为基准表 8 protected $_link = array( 9 'attr'=>array( 10 'mapping_name'=>'attr', 11 'mapping_type'=>self::MANY_TO_MANY,//多对多关系 12 'foreign_key'=>'bid',//中间表中blog的id 13 'relation_foreign_key'=>'aid',//中间表中attr的id 14 'relation_table'=>'crm_blog_attr' 15 ),//定义关联表 16 'cate'=>array( //博文和属性 一对多关系 17 'mapping_name'=>'cate', 18 'mapping_type'=>self::BELONGS_TO,//多(cate)关联一(blog)用 HAS_MANY,一(blog)关联多(cate)用BELONGS_TO 19 'foreign_key'=>id, //blog表中的博文id 20 'mapping_fields'=>'name', //只读取name字段 21 'as_fields'=>'name:cate' //把cate表的name字段取出作为主表blod的字段展示并且把name改成cate(避免字段重复) 22 ) 23 ); 24 25 //获取博文列表(包括没有删除的博文和回收站的博文,通过参数$type区分) 26 public function get_blogs($type = 0) { //默认是没有删除的博文 27 $field = array('cid','del'); 28 $where = array('del'=>$type); 29 return $this->field($field,true)->relation(array('cate','attr'))->where($where)->select(); 30 } 31 }
说明:
Line 7:如果 Model 类的文件名是 BlogModel.class.php ,则不需要定义 protected $tableName = 'blog',否则要加上定义,因为实例化 Model 类时找不到 blogrelation 表,只有 blog 表
实例化 Model 类
控制器 ./Application/Admin/Controller/BlogController.class.php :
1 <?php 2 namespace Admin\Controller; 3 use Admin\Common\Category; 4 5 class BlogController extends CommonController{ 6 //博文列表 7 public function index() { 8 $this->blog = D('BlogRelation')->get_blogs(); 9 $this->display(); 10 } 11 12 //添加博文 13 public function add_blog() { 14 //博文分类 15 $cate = M('cate')->order('sort')->select(); 16 $this->cate = Category::level($cate);//一维数组无限极分类 17 18 //博文属性 19 $this->attr = M('attr')->select(); 20 21 $this->display(); 22 } 23 24 //添加博文表单处理 25 public function add_blog_handle() { 26 //echo '<pre>';print_r($_POST); 27 $data = array( 28 'title'=>I('title'), 29 'content'=>I('content'), 30 'time'=>time(), 31 'click'=>I('click', 0, 'int'), 32 'cid'=>I('cid'), 33 ); 34 35 //属性插入到博文-属性中间表 开始 36 /*if(isset($_POST['aid'])) { 37 foreach($_POST['aid'] as $v) { 38 $data['attr'][] = $v;//attr:关联表名称 39 } 40 }*/ 41 42 /*D('BlogRelation')->relation(true)->add($data);*/ 43 //$this->display('blog');//用于查看trace信息(config.php定义) 44 //属性插入到博文-属性中间表 结束 45 46 //自定义插入关联表(不使用关联模型)开始,哪种方法都可以 47 if($bid = M('blog')->add($data)) { 48 if(isset($_POST['aid'])) { 49 $sql = 'INSERT INTO `'.C('DB_PREFIX').'blog_attr` (bid,aid) VALUES '; 50 foreach($_POST['aid'] as $v) { 51 $sql .= '('.$bid.','.$v.'),'; 52 } 53 $sql = rtrim($sql,','); 54 M()->execute($sql); 55 } 56 $this->success('添加成功', U('Admin/Blog/index')); 57 } else { 58 $this->error('添加失败'); 59 } 60 //自定义插入关联表(不使用关联模型)结束 61 62 } 63 64 65 //删除到回收站/还原 66 public function to_recycle_bin () { 67 $id = (int)$_GET['id']; 68 $type = (int)$_GET['type']; 69 $update = array( 70 'id'=>$id, 71 'del'=>$type 72 ); 73 $msg = $type ? '删除' : '还原'; 74 $location = $type ? U('Admin/Blog/index','','') : U('Admin/Blog/recycle_bin','',''); 75 if(M('blog')->save($update)) { 76 $this->success($msg.'成功' ,$location); 77 } else { 78 $this->error($msg.'失败'); 79 } 80 } 81 82 //回收站 83 public function recycle_bin() { 84 $this->blog = D('BlogRelation')->get_blogs(1); 85 $this->display('index'); 86 } 87 88 //彻底删除 89 public function delete() { 90 $id = (int)$_GET['id']; 91 //使用关联模型删除 或 手动删除 92 if(D('BlogRelation')->relation('attr')->delete($id)) { 93 $this->success('删除成功',U('Admin/Blog/recycle_bin')); 94 } else { 95 $this->error('删除失败'); 96 } 97 } 98 99 //清空回收站 100 }
转载于:https://www.cnblogs.com/dee0912/p/5189740.html
ThinkPHP 3.2.3 关联模型的使用相关推荐
- ThinkPHP关联模型详解
在ThinkPHP中,关联模型更类似一种mysql中的外键约束,但是外键约束更加安全,缺点却是在写sql语句的时候不方便,ThinkPHP很好得解决了这个问题.但是很多人不动关联模型的意思.现在就写个 ...
- thinkphp 关联模型配置代码
原文:thinkphp 关联模型配置代码 <?php /*** 公司与部门关联模型*/ class CompanyRelationModel extends RelationModel{//主表 ...
- Thinkphp 关联模型和试图模型区别
关联模型主要在多表操作时使用,比如 user表,user_role表,role表 user_role字段:uid,rid,它作为中间表,负责将user和role之间的,1对1,1对多,多对多的关系进行 ...
- ThinkPHP 3.2.3 视图模型的使用
ThinkPHP 3.2.3 试图模型的手册地址是:http://www.kancloud.cn/manual/thinkphp/1781 实例 需求:在博客列表页读取博客的(id.标题.摘要.发布时 ...
- thinkPHP6.0入门笔记(七)——关联模型
thinPHP6.0的关联模型及关联方法 1.一对一关联 1.1一对一关联查询 1.2一对一关联新增 1.3一对一关联删除 1.4一对一关联修改 2.一对多关联 2.1一对多关联模型常用方法 3.多对 ...
- thinkphp3.2 关联模型 具体实现
果然还是我太弱了,看了好多教程都没明白zen'怎么回事,每次轮到自己操作的时候就会sh失败. 记录下来,成功了方便以后自己使用. 问题1:数据库需要做xia相应的关系绑定吗?比如设置外键主键什么的? ...
- php中id如何与删除关联,ThinkPHP查询语句与关联查询用法实例
这篇文章主要介绍了ThinkPHP查询语句与关联查询用法,以实例的形式常见的查询方法,包括数组作为查询条件及对象方式来查询等技巧,需要的朋友可以参考下 本文实例讲述了ThinkPHP查询语句与关联查询 ...
- Laravel-admin 使用表单动态地保存一个关联模型(源码探究到功能实现)
我的个人博客:逐步前行STEP 有时候我们使用Laravel-admin管理数据时,需要保存一些通过程序运算出来的数据,而不只是存储写在表单中的数据,也就是需要在保存数据前可以设置或改变数据. 比如存 ...
- Laravel Eloquent关联模型查询设置查询条件与指定字段
我的个人博客:逐步前行STEP 1.直接获取关联模型: 1)在get中指定字段: $user->posts()->where('created_at','>',date('Y-m-d ...
最新文章
- 做项目需要使用的工具类
- Android之上下文菜单创建
- 【UIKit】UITableView 1
- 初学者用涩性胶皮好还是粘性_乒乓球胶皮的保养方法
- [html] HTML5如何监听video的全屏和退出全屏?
- 华硕win10键盘失灵_Win10 进阶用法汇总
- Flink Weekly | 每周社区动态更新
- ContactsProvider的使用
- PHP中smart原则,什么是smart原则(smart原则适用于哪些内容)
- CentOS7 安装 7-zip 压缩工具
- wangEditor上传不了图片
- Xmind各种删除图标的方法
- gnuplot绘图程序中对线型(linetype)、点型(pointtype)、线条宽度(linewidth)、点大小(pointsize)、图样
- python studing DAY1
- 有了LIGO,引力波不仅可以被探测,还可以被发射!
- 使用DQL命令查询数据(总篇1~3)
- 如何打通企业数据,实现数智化转型
- 王者荣耀微信哪个服务器人多,王者荣耀微信区和qq区哪个厉害
- echarts中国地图china.js下载
- 东师理想运维工具开发路线图(第一阶段)