Model层的建立
create.php
update.php
_view.php
_search.php
<?php
class AdminRole extends CActiveRecord
{
public function tableName()
{
return '{{admin_role}}';
}
public function rules()
{
return array(
array('rolename, description', 'required'),
array('rolename', 'length', 'max'=>50),
array('roleid, rolename, description', 'safe', 'on'=>'search'),
);
}
public function relations()
{
return array(
);
}
public function attributeLabels()
{
return array(
'roleid' => 'Roleid',
'rolename' => 'Rolename',
'description' => 'Description',
);
}
public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('roleid',$this->roleid);
$criteria->compare('rolename',$this->rolename,true);
$criteria->compare('description',$this->description,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
public static function model($className=__CLASS__)
{
return parent::model($className);
}
}
控制器写法
<?php
class AdminRoleController extends Controller
{
/**
* 根据编号查看详细信息
* @param $id
* @throws CHttpException
*/
public function actionView($id)
{
$this->render('view',array(
'model'=>$this->loadModel($id),
));
}
/**
* 新增
*/
public function actionCreate()
{
$model=new AdminRole;
if(isset($_POST['AdminRole']))
{
$model->attributes=$_POST['AdminRole'];
if($model->save())
$this->redirect(array('view','id'=>$model->roleid));
}
$this->render('create',array(
'model'=>$model,
));
}
/**
* 编辑
* @param $id 编号
* @throws CHttpException
*/
public function actionUpdate($id)
{
$model=$this->loadModel($id);
if(isset($_POST['AdminRole']))
{
$model->attributes=$_POST['AdminRole'];
if($model->save())
$this->redirect(array('view','id'=>$model->roleid));
}
$this->render('update',array(
'model'=>$model,
));
}
/**
* 删除
* @param $id 编号
* @throws CHttpException
*/
public function actionDelete($id)
{
$this->loadModel($id)->delete();
if(!isset($_GET['ajax']))
$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
}
public function actionIndex()
{
$dataProvider=new CActiveDataProvider('AdminRole');
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
}
/**
* 管理页面
*/
public function actionAdmin()
{
/**
* search 查询
*/
$model=new AdminRole('search');
/**
* 创建缓存
*/
Yii::app()->redis_cache->set('adminRole',$model);
$adminRole = Yii::app()->redis_cache->get('adminRole');
$model->unsetAttributes();
$adminRole->unsetAttributes();
if(isset($_GET['AdminRole']))
{
$model->attributes=$_GET['AdminRole'];
$adminRole->attributes = $_GET['AdminRole'];
}
$this->render('admin',array(
'model'=>$adminRole,
));
}
public function loadModel($id)
{
$model=AdminRole::model()->findByPk($id);
if($model===null)
throw new CHttpException(404,'The requested page does not exist.');
return $model;
}
protected function performAjaxValidation($model)
{
if(isset($_POST['ajax']) && $_POST['ajax']==='admin-role-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
}
}
View/adminRole
admin.php
<?php
/* @var $this AdminRoleController */
/* @var $model AdminRole */
Yii::app()->clientScript->registerScript('search', "
$('.search-button').click(function(){
$('.search-form').toggle();
return false;
});
$('.search-form form').submit(function(){
$('#admin-role-grid').yiiGridView('update', {
data: $(this).serialize()
});
return false;
});
");
?>
<h1>角色管理</h1>
<?php echo CHtml::link('Advanced Search','#',array('class'=>'search-button')); ?>
<div class="search-form" style="display:none">
<?php $this->renderPartial('_search',array(
'model'=>$model,
)); ?>
</div>
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'admin-role-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'roleid',
'rolename',
'description',
array(
'class'=>'CButtonColumn',
),
),
)); ?>
create.php
<?php
/* @var $this AdminRoleController */
/* @var $model AdminRole */
$this->breadcrumbs=array(
'Admin Roles'=>array('index'),
'Create',
);
$this->menu=array(
array('label'=>'List AdminRole', 'url'=>array('index')),
array('label'=>'Manage AdminRole', 'url'=>array('admin')),
);
?>
<h1>角色添加</h1>
<?php $this->renderPartial('_form', array('model'=>$model)); ?>
update.php
<h1>修改角色 <?php echo $model->roleid; ?></h1>
<?php $this->renderPartial('_form', array('model'=>$model)); ?>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'admin-role-form',
'enableAjaxValidation'=>false,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'rolename'); ?>
<?php echo $form->textField($model,'rolename',array('size'=>50,'maxlength'=>50)); ?>
<?php echo $form->error($model,'rolename'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'description'); ?>
<?php echo $form->textArea($model,'description',array('rows'=>6, 'cols'=>50)); ?>
<?php echo $form->error($model,'description'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
_view.php
<div class="view">
<b><?php echo CHtml::encode($data->getAttributeLabel('roleid')); ?>:</b>
<?php echo CHtml::link(CHtml::encode($data->roleid), array('view', 'id'=>$data->roleid)); ?>
<br />
<b><?php echo CHtml::encode($data->getAttributeLabel('rolename')); ?>:</b>
<?php echo CHtml::encode($data->rolename); ?>
<br />
<b><?php echo CHtml::encode($data->getAttributeLabel('description')); ?>:</b>
<?php echo CHtml::encode($data->description); ?>
<br />
</div>
_search.php
<?php
/* @var $this AdminRoleController */
/* @var $model AdminRole */
/* @var $form CActiveForm */
?>
<div class="wide form">
<?php $form=$this->beginWidget('CActiveForm', array(
'action'=>Yii::app()->createUrl($this->route),
'method'=>'get',
)); ?>
<div class="row">
<?php echo $form->label($model,'roleid'); ?>
<?php echo $form->textField($model,'roleid'); ?>
</div>
<div class="row">
<?php echo $form->label($model,'rolename'); ?>
<?php echo $form->textField($model,'rolename',array('size'=>50,'maxlength'=>50)); ?>
</div>
<div class="row">
<?php echo $form->label($model,'description'); ?>
<?php echo $form->textArea($model,'description',array('rows'=>6, 'cols'=>50)); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Search'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- search-form -->