I am trying to create a simple filter using a form and CActiveForm dropDownList(). I cannot keep the selected list item active after the form is posted and I cannot see what I am missing.
Model
class Art extends CActiveRecord
{
public $locationID;
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return Art 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 'art';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('name, frame, categoryID, sizeID, locationID, statusID, note', 'required'),
array('frame, categoryID, sizeID, locationID, statusID, typeID, collectorID, reproductionID', 'numerical', 'integerOnly'=>true),
array('name', 'length', 'max'=>100),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, name, frame, categoryID, sizeID, locationID, statusID, typeID, collectorID, reproductionID, note', 'safe'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'status' => array(self::BELONGS_TO, 'Status', 'statusID'),
'category' => array(self::BELONGS_TO, 'Category', 'categoryID'),
'type' => array(self::BELONGS_TO, 'Type', 'typeID'),
'collector' => array(self::BELONGS_TO, 'Collector', 'collectorID'),
'location' => array(self::BELONGS_TO, 'Location', 'locationID'),
'reproduction' => array(self::BELONGS_TO, 'Reproduction', 'reproductionID'),
'size' => array(self::BELONGS_TO, 'Size', 'sizeID'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'name' => 'Name',
'frame' => 'Frame',
'categoryID' => 'Category',
'sizeID' => 'Size',
'locationID' => 'Location',
'statusID' => 'Status',
'typeID' => 'Type',
'collectorID' => 'Collector',
'reproductionID' => 'Reproduction',
'note' => 'Note',
);
}
}
Controller
public function accessRules()
{
return array(
array('allow', // allow all users to perform 'index' and 'view' actions
'actions'=>array('index','view'),
'users'=>array('*'),
),
array('allow', // allow authenticated user to perform 'create' and 'update' actions
'actions'=>array('create','update'),
'users'=>array('@'),
),
array('allow', // allow admin user to perform 'admin' and 'delete' actions
'actions'=>array('admin','delete'),
'users'=>array('admin'),
),
array('deny', // deny all users
'users'=>array('*'),
),
);
}
public function actionIndex()
{
$model=new Art;
$condition='statusID=1';
if(isset($_POST['Art'])){
if($_POST['Art']['locationID']>0)
$condition.=' AND locationID='.$_POST['Art']['locationID'];
}
$dataProvider=new CActiveDataProvider('Art',array(
'criteria'=>array(
'condition'=>$condition,
'order'=>'name',
),
'pagination'=>array(
'pageSize'=>12,
),
));
//$dataProvider->setPagination(false);
//$rows=$dataProvider->getTotalItemCount();
//$dataProvider=$dataProvider->getData();
$this->render('index',array(
'dataProvider'=>$dataProvider,
'model'=>$model,
));
}
View
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'art-form',
'enableAjaxValidation'=>false,
)); ?>
<div class="row">
<?php echo $form->dropDownList($model,'locationID',array(0=>'All',1=>'Studio',5=>'Storage - New York',6=>'Stotage - Boston')); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Filter'); ?>
</div>
<?php $this->endWidget(); ?>