一、视图
<div class="form-horizontal">
<?php $form = $this->beginWidget('CActiveForm', array('id' => 'course', 'action' => '/admin/mp/')); ?>
<div class="form-group">
<div class="col-md-2">
<?php echo $form->textField($model, 'title', array('class' => 'form-control', 'placeholder' => '标题')); ?>
</div>
<div class="col-md-2">
<?php echo $form->textField($model, 'name', array('class' => 'form-control', 'placeholder' => '名字')); ?>
</div>
<div class="col-md-2">
<?php echo $form->dropDownList($model, 'type', array('' => '类别') + $model->getTypeList(), array('class' => 'form-control', 'placeholder' => '类别')); ?>
</div>
<div class="col-md-1">
<?php echo CHtml::submitButton('查询', array('class' => 'btn btn-default btn-primary')); ?>
</div>
<div class="col-md-1">
<a href="/admin/user/edit" class="btn btn-default">添加矩阵</a>
</div>
</div>
<?php $this->endWidget(); ?>
</div>
二、模型
class Mp extends CActiveRecord
{ const AFFAIRS = 1;
const BUSINESS = 2; public function tableName()
{
return 'mp';
} public static function model($className = __CLASS__)
{
return parent::model($className);
} public function getTypeList()
{
return array(
self::AFFAIRS => '政务',
self::BUSINESS => '商业',
);
} public function getTypeName()
{
$arr = $this->getTypeList();
if (isset($arr[$this->type])) {
return $arr[$this->type];
}
return '未知';
}
public function rules()
{
return array(
array('title,name,type', 'safe', 'on' => 'search'),
);
} }
三、控制器
public function actionIndex()
{
$model = new Mp('search');
if (isset($_POST['Mp'])) {
$model->setAttributes($_POST['Mp']);
}
$criteria = new CDbCriteria();
$criteria->addSearchCondition('title', $model->title);
$criteria->addSearchCondition('name', $model->name);
$criteria->addSearchCondition('type', $model->type);
$count = Mp::model()->count($criteria);
$pager = new CPagination($count);
$pager->pageSize = $this->pageSize;
$pager->applyLimit($criteria); $list = Mp::model()->findAll($criteria);
$this->render('index',array('model'=>$model,'list'=>$list,'pager'=>$pager)); }