yii增删改查

首先要在protected/models下面创建相应的数据表模块(假如有一个表 prex_sq_huodong)
class SqHuodong extends CActiveRecord
{
    /**
    * Returns the static model of the specified AR class.
    * @param string $className active record class name.
    * @return ComClass the static model class
    */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }


    /**
    * @return string the associated database table name
    */
    public function tableName()
    {
        return '{{sq_huodong}}';
    }


    public function rules() 
    {
        return array(
        array('title', 'length', 'max'=>25),
        array('content', 'length', 'min'=>6, 'max'=>255),
        array('add_time', 'numerical', 'integerOnly'=>true),
        );
    }
}


    获取全部列表
    $criteria = new CDbCriteria;

    //分页
        $count = SqHuodong::model()->count($criteria);
        $pager = new CPagination($count);
        $pager->pageSize = 2;
        $pager->applyLimit($criteria);

    //分页结束
        $list = SqHuodong::model()->findAll($criteria);

        $this->render('/my_huodong/sqhuodong', array(
            'list' => $list,
            'count' => $count,
            'pages' => $pager,
        ));
    }


    //添加数据
    public function actionAddsqhuodong() 
    {
        $model = new SqHuodong();
        if(isset($_POST['SqHuodong'])) 
        {
            $model->attributes = $_POST['SqHuodong'];
            $model->attributes = array(
                'add_time' => time(),
            );

            if ($model->save()) 
            {
                $this->redirect(array('admin'));
            } 
        }

        $this->render('/my_huodong/add_sqhuodong', array(
            'model' => $model,
        ));
    }

    //修改
    public function actionUpdate($id) 
    {

        $model = SqHuodong::model()->findByPk($id);
        if(!$model) 
        {
            $this->redirect_message(array(
                'message'=>'活动信息不存在!',
            ));
        }

        if(isset($_POST['SqHuodong'])) 
        {
            $model->attributes = $_POST['SqHuodong'];
            $model->attributes = array(
                'add_time' => time(),
            );

            if ($model->save()) 
            {
                $this->redirect(array('admin'));
            }
        }

        $this->render('/my_huodong/add_sqhuodong', array(
            'model' => $model,
        ));
    }

    //删除
    public function actionDel($id) 
    {
        $model = SqHuodong::model()->find('id=:id', array(':id'=>$id));
        //$model = SqHuodong::model()->find('id=?', array('id'=>$id));
        if(!$model) 
        {
            $this->redirect_message(array(
                'message'=>'删除失败,活动信息不存在!',
            ));
        } 
        else 
        {
            $model->delete();
            $this->redirect_message(array(
                'message' =>'删除成功!',
            ));
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值