首先要在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' =>'删除成功!',
));
}
}
yii增删改查
最新推荐文章于 2019-04-19 14:09:00 发布