Hi All,
I’m a newbie to Yii and PHP. I want to use a dropDownList in CGridView to filter database (MySQL). I had no problem in columns with foreignKey or relations, it displays dropDownList options and filtered the correct data. However the other columns (w/out foreignKey), although it displays the dropDown button and list down all options but after you select one the result is always “No results found”. Is it possible to remove duplicates from dropDownList options and why the filter in this column is not working?
Thanks in Advance.
CONTROLLER:
/**
* Manages all models.
*/
public function actionAdmin()
{
$model=new Scaffold('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['Scaffold']))
$model->attributes=$_GET['Scaffold'];
$this->render('admin',array(
'model'=>$model,
));
}
MODEL:
/**
* @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('id, department_id, facility_id, location, type_id, duty_id, status_id, date_erected', 'required'),
array('id, department_id, facility_id, type_id, duty_id, status_id, work_order, deluge_id, los_id, deboard_id', 'numerical', 'integerOnly'=>true),
array('duty_id', 'in', 'range'=>self::getAllowedDutyRange()),
array('status_id', 'in', 'range'=>self::getAllowedStatusRange()),
array('deluge_id', 'in', 'range'=>self::getAllowedDelugeRange()),
array('los_id', 'in', 'range'=>self::getAllowedLosRange()),
array('deboard_id', 'in', 'range'=>self::getAllowedDeboardRange()),
array('weight, erect_manhrs, dismantle_manhrs', 'length', 'max'=>11),
array('requested_by, built_by', 'length', 'max'=>64),
array('id', 'unique'),
array('inspection_date, date_dismantled, other_details, material_list', 'safe'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, department_id, facility_id, location, type_id, duty_id, status_id, weight, work_order, erect_manhrs, dismantle_manhrs, date_erected, inspection_date, date_dismantled, requested_by, built_by, other_details, deluge_id, los_id, deboard_id, material_list, create_time, create_user_id, update_time, update_user_id', 'safe', 'on'=>'search'),
);
}
/**
* @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(
'inspections' => array(self::HAS_MANY, 'Inspection', 'scaffold_id'),
'type' => array(self::BELONGS_TO, 'Type', 'type_id'),
'department' => array(self::BELONGS_TO, 'Department', 'department_id'),
'facility' => array(self::BELONGS_TO, 'Facility', 'facility_id'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'Reference Number',
'department_id' => 'Department Requestor',
'facility_id' => 'Facility',
'location' => 'Location',
'type_id' => 'Type of Scaffold',
'duty_id' => 'Design Loading',
'status_id' => 'Status',
'weight' => 'Weight (Kg.)',
'work_order' => 'Work Order',
'erect_manhrs' => 'Manhours to Build',
'dismantle_manhrs' => 'Manhours to Dismantle',
'date_erected' => 'Date Erected',
'inspection_date' => 'Next Inspection Due',
'date_dismantled' => 'Date Dismantled',
'requested_by' => 'Requested By',
'built_by' => 'Built By',
'other_details' => 'Other Details',
'deluge_id' => 'Blocking Deluge?',
'los_id' => 'Blocking LOS?',
'deboard_id' => 'Deboarded?',
'material_list' => 'Material List',
'create_time' => 'Create Time',
'create_user_id' => 'Create User',
'update_time' => 'Update Time',
'update_user_id' => 'Update User',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('department_id',$this->department_id);
$criteria->compare('facility_id',$this->facility_id);
$criteria->compare('location',$this->location,true);
$criteria->compare('type_id',$this->type_id);
$criteria->compare('duty_id',$this->duty_id);
$criteria->compare('status_id',$this->status_id);
$criteria->compare('weight',$this->weight,true);
$criteria->compare('work_order',$this->work_order);
$criteria->compare('erect_manhrs',$this->erect_manhrs,true);
$criteria->compare('dismantle_manhrs',$this->dismantle_manhrs,true);
$criteria->compare('date_erected',$this->date_erected,true);
$criteria->compare('inspection_date',$this->inspection_date,true);
$criteria->compare('date_dismantled',$this->date_dismantled,true);
$criteria->compare('requested_by',$this->requested_by,true);
$criteria->compare('built_by',$this->built_by,true);
$criteria->compare('other_details',$this->other_details,true);
$criteria->compare('deluge_id',$this->deluge_id);
$criteria->compare('los_id',$this->los_id);
$criteria->compare('deboard_id',$this->deboard_id);
$criteria->compare('material_list',$this->material_list,true);
$criteria->compare('create_time',$this->create_time,true);
$criteria->compare('create_user_id',$this->create_user_id);
$criteria->compare('update_time',$this->update_time,true);
$criteria->compare('update_user_id',$this->update_user_id);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'sort' => array(
'defaultOrder' => 'id DESC',
),
'pagination' => array(
'pageSize' => 10,
),
));
}
ADMIN.PHP:
<?php $this->widget(‘zii.widgets.grid.CGridView’, array(
'id'=>'scaffold-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
array(
'name' => 'id',
'htmlOptions' => array('width'=>60)
),
array(
'name' => 'department_id',
'value' => '$data->department->name',
'filter' => CHtml::listData(Department::model()->findAll(),'id','name'),
'htmlOptions' => array('width'=>95)
),
array(
'name' => 'facility_id',
'value' => '$data->facility->name',
'filter' => CHtml::listData(Facility::model()->findAll(),'id','name'),
'htmlOptions' => array('width'=>55)
),
'location',
array(
'name' => 'status_id',
'value' => '$data->getStatusText()',
'filter' => $model->getStatusOptions(),
'htmlOptions' => array('width'=>90)
),
array(
'name' => 'date_erected',
'filter' => CHtml::listData(Scaffold::model()->findAll(array('order'=>'date_erected')),'id','date_erected'),
'htmlOptions' => array('width'=>90)
),
array(
'name' => 'inspection_date',
'filter' => CHtml::listData(Scaffold::model()->findAll(array('order'=>'inspection_date')),'id','inspection_date'),
'htmlOptions' => array('width'=>90)
),
array(
'class'=>'CButtonColumn',
),
),
)); ?>