标签:
我们删除要在分类模型中添加一个_before_delete的钩子函数,而且在删除一个分类时候,如果这个分类有子分类就不允许删除
model层代码如下所示:
<?php namespace Admin\Model; use Think\Model; class CategoryModel extends Model{ protected $_validate = array( //array(验证字段1,验证规则,错误提示,[验证条件,附加规则,验证时间]), array(‘cate_name‘,‘require‘,‘栏目中文名不能为空!‘,1,‘regex‘,3), //默认情况下用正则进行验证, //这里的1是必须验证 //regex正则验证,定义的验证规则是一个正则表达式(默认) //3全部情况下验证 array(‘cate_ename‘,‘require‘,‘栏目英文名不能为空!‘), //默认情况下用正则进行验证 ); //定义一个方法,获取树状的分类信息 public function catTree(){ $cats = $this->select(); //通过tree里面的方法进行排序 return $this->tree($cats); } public function tree($arr,$pid=0,$level=0){ //$level是自己定义出来的 static $tree; foreach($arr as $v){ if($v[‘cate_pid‘]==$pid){ //说明找到,保存 $v[‘level‘] = $level; $tree[] = $v; //继续找 $this -> tree($arr,$v[‘cate_id‘],$level+1); //这里的cate_id表的id } } return $tree; } //getSubIds函数的作用是获取一个分类所有子分类的id public function getSubIds($cate_id){ //找所有分类 $data = $this -> select(); //递归找出所有子分类的id return $this -> _getSubIds($data,$cate_id); } //递归查找 private function _getSubIds($data,$cate_pid,$isclear=false){ static $ret; //因为每次调用他静态变量他都会追加,所以我们让他格式化一下 if($isclear){ $ret = array(); } foreach($data as $k=>$v){ if($v[‘cate_pid‘] == $cate_pid){ $ret[] = $v[‘cate_id‘]; //再找这条分类的子分类 $this -> _getSubIds($data,$v[‘cate_id‘]); } } return $ret; } //_before_delete TP的钩子函数 protected function _before_delete($option){ //查询有没有子分类 $ck = $this -> getSubIds($option[‘where‘][‘cate_id‘]); if($ck){ $this -> error = "该栏目有子分类不能删除"; return false; } } }
然后我们就到控制器来完善下即可:
<?php namespace Admin\Controller; use Think\Controller; class CategoryController extends Controller { public function lists(){ $mod = D(‘Category‘)->catTree(); $this -> assign(‘mod‘,$mod); $this->display(); } public function add(){ $cmod = D(‘Category‘); if(IS_POST){ $data[‘cate_name‘] = I(‘cate_name‘); $data[‘cate_ename‘] = I(‘cate_ename‘); $data[‘cate_keywords‘] = I(‘cate_keywords‘); $data[‘cate_desc‘] = I(‘cate_desc‘); $data[‘cate_type‘] = I(‘cate_type‘); $data[‘cate_pid‘] = I(‘cate_pid‘); $data[‘cate_content‘] = I(‘cate_content‘); if($_FILES[‘cate_pic‘][‘tmp_name‘]!=‘‘){ $upload = new \Think\Upload();// 实例化上传类 $upload->maxSize = 3145728 ;// 设置附件上传大小 $upload->exts = array(‘jpg‘, ‘gif‘, ‘png‘, ‘jpeg‘);// 设置附件上传类型 $upload->rootPath = ‘./‘; //这个一定要加否则很容易中招 $upload->savePath = ‘Public/Uploads/‘; // 设置附件上传目录 $info = $upload->uploadOne($_FILES[‘cate_pic‘]); if($info){ //这里组装sql语句,让图片融入$data[‘pic‘]里面 $data[‘cate_pic‘] = $info[‘savepath‘].$info[‘savename‘]; }else{ $this->error($upload->getError()); } } //dump($_FILES); if($cmod->create($data)){ if($cmod->add()){ $this->success(‘添加栏目成功‘,U(‘Category/lists‘)); }else{ $this ->error(‘添加栏目失败‘); } }else{ $this -> error($cmod->getError()); } return; } //因为我们在上面用了大D已经实例化对象了,所以直接以这种方式进行取出 $cateres = D(‘Category‘)->catTree(); $this -> assign(‘cateres‘,$cateres); $this -> display(); } public function edit(){ $id = I(‘cate_id‘); $vo = M(‘Category‘)->find($id); $this -> assign(‘vo‘,$vo); //栏目列表的遍历 开始 $cat = D(‘Category‘)->catTree(); $this -> assign(‘cat‘,$cat); //栏目列表的遍历 结束 //dump($cat); //主要作用是用来查取下级分类的 开始 $catemod = D(‘Category‘); //取出当前分类所有子分类的id $cates = $catemod->getSubIds($id); //将当前分类和子分类放到一起 $cates[] = $id; $this -> assign(‘cates‘,$cates); //下级分类查取 结束 //dump($cates); if(IS_POST){ $data[‘cate_id‘] = I(‘cate_id‘); $data[‘cate_name‘] = I(‘cate_name‘); $data[‘cate_ename‘] = I(‘cate_ename‘); $data[‘cate_keywords‘] = I(‘cate_keywords‘); $data[‘cate_desc‘] = I(‘cate_desc‘); $data[‘cate_type‘] = I(‘cate_type‘); $data[‘cate_pid‘] = I(‘cate_pid‘); $data[‘cate_content‘] = I(‘cate_content‘); if($_FILES[‘cate_pic‘][‘tmp_name‘]!=‘‘){ $upload = new \Think\Upload();// 实例化上传类 $upload->maxSize = 3145728 ;// 设置附件上传大小 $upload->exts = array(‘jpg‘, ‘gif‘, ‘png‘, ‘jpeg‘);// 设置附件上传类型 $upload->rootPath = ‘./‘; //这个一定要加否则很容易中招 $upload->savePath = ‘Public/Uploads/‘; // 设置附件上传目录 $info = $upload->uploadOne($_FILES[‘cate_pic‘]); if($info){ //删除原来的图片 $cp = M(‘Category‘)->find($id); unlink($cp[‘cate_pic‘]); // 上传成功 获取上传文件信息 //这里组装sql语句,让图片融入$data[‘pic‘]里面 $data[‘cate_pic‘] = $info[‘savepath‘].$info[‘savename‘]; }else{ $this->error($upload->getError()); } } $mod =D("Category"); if($mod-> create($data)){ if($mod->save()){ $this->success(‘修改栏目成功‘,U(‘Category/lists‘)); }else{ $this ->error(‘修改栏目失败‘); } }else{ $this->error($brandModel->getError()); } } $this->display(); } public function del(){ $mod = D("Category"); if($mod->delete(I(‘get.cate_id‘,0)) !== false){ $this -> success("删除成功",U(‘Category/lists‘)); }else{ $this -> error($mod->getError()); } } }
视图那就简单多啦,只需要在删除那里发送yigecate_id过来即可:
<!doctype html> <html> <head> <meta charset="UTF-8"> <title>后台管理</title> <link rel="stylesheet" type="text/css" href="__PUBLIC__/Admin/css/common.css"/> <link rel="stylesheet" type="text/css" href="__PUBLIC__/Admin/css/main.css"/> <script type="text/javascript" src="__PUBLIC__/Admin/js/libs/modernizr.min.js"></script> <script type="text/javascript" src="__PUBLIC__/Admin/js/jq18m.js"></script> <script type="text/javascript" src="__PUBLIC__/Admin/js/hileft.js"></script> </head> <body> <include file="Public/header" /> <div class="container clearfix"> <include file="Public/left" /> <!--/sidebar--> <div class="main-wrap"> <div class="crumb-wrap"> <div class="crumb-list"><i class="icon-font"></i><a href="/jscss/admin">首页</a><span class="crumb-step">></span><span class="crumb-name">作品管理</span></div> </div> <div class="search-wrap"> <div class="search-content"> <form action="/jscss/admin/design/index" method="post"> <table class="search-tab"> <tr> <th width="120">选择分类:</th> <td> <select name="search-sort" id=""> <option value="">全部</option> <option value="19">精品界面</option><option value="20">推荐界面</option> </select> </td> <th width="70">关键字:</th> <td><input class="common-text" placeholder="关键字" name="keywords" value="" id="" type="text"></td> <td><input class="btn btn-primary btn2" name="sub" value="查询" type="submit"></td> </tr> </table> </form> </div> </div> <div class="result-wrap"> <form name="myform" id="myform" method="post" action="__CONTROLLER__/"> <div class="result-title"> <div class="result-list"> <a href="insert.html"><i class="icon-font"></i>新增作品</a> <a id="batchDel" href="javascript:void(0)"><i class="icon-font"></i>批量删除</a> <a id="updateOrd" href="javascript:void(0)"><i class="icon-font"></i>更新排序</a> </div> </div> <div class="result-content"> <table class="result-tab" width="100%"> <tr> <th>栏目id</th> <th>栏目中文名称</th> <th>栏目英文名称</th> <th>栏目图片</th> <th>上级栏目</th> <th>栏目类型</th> <th>操作</th> </tr> <volist name="mod" id="vo"> <tr> <td>{$vo[‘cate_id‘]}</td> <td>{$vo[‘level‘]|str_repeat="▲ ",###} {$vo[‘cate_name‘]}</td> <td>{$vo[‘cate_ename‘]}</td> <if condition="$vo[‘cate_pic‘] eq ‘‘"> <td>抱歉目前没有图片数据</td> <else /> <td><img src="/{$vo[‘cate_pic‘]}" width="100" height="60"></td> </if> <td>{$vo[‘cate_pid‘]}</td> <if condition="$vo[‘cate_type‘] eq 0"> <td>封面栏目</td> <elseif condition="$vo[‘cate_type‘] eq 1" /> <td>列表栏目</td> <elseif condition="$vo[‘cate_type‘] eq 2" /> <td>产品栏目</td> </if> <td> <a class="link-update" href="__MODULE__/Category/edit/cate_id/{$vo[‘cate_id‘]}">修改</a> <a class="link-del" href="__MODULE__/Category/del/cate_id/{$vo[‘cate_id‘]}" onclick="return confirm(‘您确定要删除该栏目么?‘);" >删除</a> </td> </tr> </volist> </table> <input type="submit" name="" value="删除" style="margin-left: 45px; margin-top: 10px;"> <div class="list-page"> 2 条 1/1 页</div> </div> </form> </div> </div> <!--/main--> <script> //在 jq 1.8.3测试成功 $("#selall").click(function(){ if($(this).attr("checked")){ $(‘.selall‘).attr("checked","checked"); }else{ $(‘.selall‘).removeAttr("checked"); } }); </script> </div> </body> </html>
夺命雷公狗ThinkPHP项目之----企业网站12之栏目的删除完成
标签:
原文地址:http://www.cnblogs.com/leigood/p/5426269.html