mysql - 如何在 Yii 中为 ENUM 列实现 CGridView 表的过滤?

标签 mysql yii enums cgridview yii-cactiverecord

在 Yii 应用程序中,我将数据显示为表格。我使用 CGridView,它为我处理(基于搜索的)数据过滤和排序。它有效。

现在我向user 数据库表添加了一个ENUMstatus。在网格中,我可以对其进行排序和过滤,但只能按表中的值进行排序和过滤。好吧,这是有道理的。但用户实际上并不知道它是如何保存在数据库中并与标签一起使用(并希望按标签进行排序和过滤)。

有没有办法为 Yii 中的 ENUM 数据库表列提供按自定义标签的排序和过滤(使用 CActiveRecord 模型和 CGridView 生成数据网格)?


数据库表

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`)
);

用户模型

/**
 * ...
 * @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);
    }

}

用户 Controller

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,
        ));
    }

    ...

}

查看

...

<?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(
                )
            ),
        ),
    ),
));

最佳答案

型号:

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';
  }
}

查看:

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

就这样

关于mysql - 如何在 Yii 中为 ENUM 列实现 CGridView 表的过滤?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31473268/

相关文章:

mysql - 使用 TableGateway 在 ZF2 中进行左连接

PHP 函数从数据库中提取字段值

java - 将对象转换为枚举 JAVA

C++ 枚举重新定义和 C++0x

c++ - C++0x 中 C++ 枚举的基础类型

MySQL - 表指定两次作为更新目标和单独的数据源

php - eclipse : "PHP Fatal error: Call to undefined function mysqli_connect()"

PHP fatal error : Unknown typehint

sql - Yii2 和 Filter 都带有 CASES。是否可以?

php - 在 Yii 框架中读取 CSV