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

创建新的数据库的语句
创建新关于后盾网yii框架的学习小结(8)--的数据库

<?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'));
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值