ThinkPHP 3.2.3 的關聯模型(手冊地址)通常處理關聯數據表的 CURD 操做,例如關聯讀取、關聯寫入、關聯刪除等。php
博客管理模塊關於博客有 4 張數據表:博客表 crm_blog:sql
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:thinkphp
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)app
類別表 crm_cate:this
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)關係spa
在 Admin 應用下建立 Model 類:./Application/Admin/Model/BlogRelationModel.class.php:code
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 }
說明:blog
Line 7:若是 Model 類的文件名是 BlogModel.class.php ,則不須要定義 protected $tableName = 'blog',不然要加上定義,由於實例化 Model 類時找不到 blogrelation 表,只有 blog 表ci
實例化 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 }