mysqlyiienumscgridviewyii-cactiverecord

How to implement filtering of a CGridView table for an ENUM columnt in Yii?


In a Yii application I display the data as a table. I use CGridView, that deals for me with (search based) data filter and sorting. It works.

Now I added an ENUM column status to the user database table. In the grid I can sort and filter this, but only by the value in the table. Well, it makes sense. But the user don't actually know, how it's saved n the database and works with (and wants to sort and filter by) labels.

Is there a way to provide sorting and filtering by custom labels for an ENUM database table column in Yii (using a CActiveRecord model and a CGridView generated data grid)?


database table

CREATE TABLE `users` (
    `userid` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
    `email` varchar(45) NOT NULL,
    ...
    `status` enum('processed', 'waiting') NOT NULL,
    PRIMARY KEY (`userid`),
    UNIQUE KEY `email_UNIQUE` (`email`)
);

User model

/**
 * ...
 * @property string $userid
 * @property string $email
 * ... 
 * @property string $status
 */
class User extends CActiveRecord
{

    public function tableName()
    {
        return 'users';
    }

    public function getFirstAndLastName(){
        return CHtml::encode($this->firstname." ".$this->lastname);
    }

    public function rules()
    {
        return array(
            ...
            array('userid, email, status', 'safe', 'on'=>'search'),
        );
    }

    public function attributeLabels()
    {
        return array(
            'userid' => 'Userid',
            'email' => 'Email',
            ...
            'status' => Yii::t('app', 'status'),
        );
    }

    public function relations()
    {
        return array(
            ...
        );
    }

    public function search()
    {
        $criteria=new CDbCriteria;

        $criteria->compare('userid',$this->userid,true);
        $criteria->compare('email',$this->email,true);
        ...
        $criteria->compare('status',$this->status,true);

        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
            "pagination"=>array(
                "pageSize"=>25
            )
        ));
    }

    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    protected function beforeSave()
    {
        return parent::beforeSave(); // TODO: Change the autogenerated stub
    }

    public function getProcessingStatus()
    {
        return Yii::t('app', $this->processing_status);
    }

}

UserController

class UserController extends Controller
{
    ...

    /**
     * Manages all models.
     */
    public function actionAdmin()
    {
//        Yii::app()->cache->flush();
        $model=new User('search');
        $model->unsetAttributes();  // clear any default values
        if(isset($_GET['User']))
            $model->attributes=$_GET['User'];

        $this->render('admin',array(
            'model'=>$model,
        ));
    }

    ...

}

view

...

<?php
$this->widget('zii.widgets.grid.CGridView', array(
    'id' => 'user-grid',
    'dataProvider' => $model->search(),
    'filter' => $model,
    'columns' => array(
        'email',
        'status',
        array(
            'class' => 'CButtonColumn',
            'template' => '{view}{update}',
            'buttons' => array(
                'view' => array(
                )
            ),
        ),
    ),
));

Solution

  • Model:

    class User extends CActiveRecord {
    
      const STATUS_PROCESSED = 'processed';
      const STATUS_WAITING = 'waiting';
    
      public static function getStatusList(){
        return array(
          self::STATUS_PROCESSED => 'Processed',
          self::STATUS_WAITING => 'Waiting',
        );
      }
    
      public function getStatusValue(){
        $list = self::getStatusList();
        return array_key_exists( $this->status, $list ) ? $list[ $this->status ] : 'Undefined';
      }
    }
    

    View:

    $this->widget('zii.widgets.grid.CGridView', array(
      // other params
      'columns' => array(
        // other columns
        array(
          'name' => 'status',
          'value' => '$data->getStatusValue()',
          'filter' => User::getStatusList(),
        ),
      )
    ));
    

    That's all