码迷,mamicode.com
首页 > 其他好文 > 详细

关于后盾网yii框架的学习小结(10)--使用AR类的增删改查

时间:2015-06-19 16:50:46      阅读:95      评论:0      收藏:0      [点我收藏+]

标签:增删改查   数据库   extends   yii   return array   

创建新的数据库的语句
技术分享

<?php 
class CategoryController extends Controller{
    public function filters(){
        return array(
                ‘accessControl‘,
            );
    }

    public function accessRules(){
        return array(
            //更加具体化
            // array(
            //  ‘allow‘,
            //  ‘actions‘=>array(‘del‘,‘add‘),
            //  ‘users‘ => array(‘admin‘)
            //  ),

            array(
                ‘allow‘,
                ‘actions‘=>array(‘index‘, ‘del‘,‘add‘, ‘edit‘),
                ‘users‘ => array(‘@‘)
                ),
            array(
                ‘deny‘,
                ‘users‘ => array(‘*‘)
                ),


            );
    }

    /**
     * 查看栏目
     */
    public function actionIndex(){
        $categoryModel = Category::model();
        $sql = "SELECT cid,cname FROM {{category}}";
        $categoryInfo = $categoryModel->findAllBySql($sql);

        $this->render(‘index‘, array(‘categoryInfo‘=>$categoryInfo));
    }

    /**
     * 添加栏目
     */
    public function actionAdd(){
        $categoryModel = new Category();

        if(isset($_POST[‘Category‘])){

            $categoryModel->attributes = $_POST[‘Category‘];

            if($categoryModel->save()){
                $this->redirect(array(‘index‘));
            }

        }
        // p($_POST);
        $this->render(‘add‘, array(‘categoryModel‘=>$categoryModel));
    }



    /**
     * 编辑栏目
     */
    public function actionEdit($cid){
        $categoryModel = Category::model();
        $categoryInfo = $categoryModel->findByPk($cid);

        // p($_POST);
        if(isset($_POST[‘Category‘])){
            $categoryInfo->attributes = $_POST[‘Category‘];
            if($categoryInfo->save()){
                $this->redirect(array(‘index‘));
            }
        }

        $this->render(‘edit‘, array(‘categoryModel‘=>$categoryInfo));
    }


    /**
     * 删除栏目
     */
    public function actionDel($cid){
        $articleModel = Article::model();

        $sql = "SELECT aid FROM {{article}} WHERE cid=$cid";
        $articleInfo = $articleModel->findBysql($sql);

        if(is_object($articleInfo)){
            Yii::app()->user->setFlash(‘hasArt‘, ‘栏目下面有文章,请先删除文章‘);
            $this->redirect(array(‘index‘));

        }else {
            if(Category::model()->deleteByPk($cid)){
                $this->redirect(array(‘index‘));
            }
        }
    }
}

关于后盾网yii框架的学习小结(10)--使用AR类的增删改查

标签:增删改查   数据库   extends   yii   return array   

原文地址:http://blog.csdn.net/rodgexue/article/details/46561951

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!