简单的博客和分类关联代码(20140509更新)

jerry thinkphp 2015年11月19日 收藏
关联代码,包括修改,分页,获取,增加,删除,修改,由于我是新手,写的不是很好,求高手指点修正:)
控制器代码(是Action里的 IndexAction.class.php):
  1. <?php
  2. class IndexAction extends Action {
  3.     //实例化博客,还有关联分类在Model里
  4.     public function index(){
  5.            //实例公共模型类
  6.            $Blog=D('Blog');
  7.            //调用分页类
  8.            import('ORG.Util.Page');
  9.            //统计分页
  10.            $count = $Blog->count();
  11.            //创建分页数
  12.            $Page = new Page($count,5);
  13.            //分页输出
  14.            $show = $Page->show();
  15.            //赋值给关联查询,并且倒序输出
  16.            $list=$Blog->relation(true)->order('id DESC')->limit($Page->firstRow.','.$Page->listRows)->select();
  17.            //赋值给列表标签
  18.            $this->assign('list',$list);
  19.            //赋值给分页标签
  20.            $this->assign('page',$show);
  21.            //创建视图
  22.            $this->display();
  23.     }
  24.     
  25.     //删除博客
  26.     public function del(){
  27.                 // 在模版里修改地址是 __APP__/你的文件(INDEX或者ADMIN)/del/id/{$x.id}
  28.          //视图$_GET提交ID
  29.          $id =$_GET['id'];
  30.          //其中ID传值,如果不写$where,而是$id 会把整个数据
  31.          $where = array('id' => $id);
  32.          //判断博客删除
  33.          if (M('Blog')->where($id)->delete()) {
  34.             $this->success('删除成功');
  35.          } else {
  36.              $this->error('删除失败');   
  37.          }  
  38.          
  39.    }
  40.   
  41.     //增加博客
  42.     Public function add () {
  43.     //显示修改数据
  44.     $this->cate =$cate = M('cate')->select();
  45.     //组装主表数据
  46.     $id = $_POST['id'];
  47.     $cid = $_POST['cid'];
  48.     //tutle博客标题 content博客内容 cid 父级分类关联 cate
  49.     $data = array(
  50.             'title' => $_POST['title'],
  51.             'content' => $_POST['content'],
  52.             'cid' => $cid,
  53.         );
  54.         //增加新数据
  55.         if ($bid = M('blog')->add($data)) {
  56.                 if (isset($_POST['cid'])) {
  57.                     $this->success('添加成功','index');
  58.                 } else {
  59.                     $this->error('添加失败'); 
  60.                 } 
  61.                 } 
  62.           //创建增加视图     
  63.           $this->display();
  64.     }

  65.     //修改
  66.     Public function mod () {
  67.                          // 在模版里修改地址是 __APP__/你的文件(INDEX或者ADMIN)/mod/id/{$x.id}
  68.         $id=$_GET['id'];
  69.         if(!empty($id)){
  70.             //获取博客文章标题和内容
  71.             $art=M('Blog');
  72.             $date=$art->getById($id);
  73.             //赋值给博客标签datas
  74.             $this->assign('datas',$date);
  75.             //获取分类
  76.             $cat=M('cate');
  77.             $list=$cat->select();
  78.             //复制给分类标签clist
  79.             $this->assign('clist',$list);
  80.         }
  81.         //创建修改模版视图
  82.         $this->display();
  83.     }

  84.     //修改提交处理
  85.     public function update(){
  86.         //create方法
  87.         $Form= M('blog');
  88.         if($Form->create()) {
  89.         $result = $Form->save();
  90.         if($result) {
  91.              //如果修改成功,跳转到首页
  92.              $this->success('操作成功!','index');
  93.         }else{
  94.              //否则修改错误
  95.              $this->error('写入错误!');
  96.         } 
  97.              //否则系统异常错误
  98.         }else{
  99.         $this->error($Form->getError());
  100.         }
  101.     }
  102. } 
模型代码(是Model里的 ,文件名BlogModel.class.php):
  1. <?php
  2. class BlogModel extends RelationModel{
  3.     protected $_link = array(

  4.          'Cate'=>array(
  5.               'mapping_type'=> BELONGS_TO,
  6.               'class_name'=>'Cate',
  7.               'mapping_name'=>'Cate',
  8.               'foreign_key'=>'cid',
  9.               //只获取关联里name内容
  10.               'mapping_fields'=>'name',
  11.               //把关联的name拿到父级上
  12.               'as_fields'=>'name',
  13.               //'as_fields' =>'id:uid,title,name',

  14.              ),
  15.     
  16.     );}
  17.     
  18. ?>