在yii CGridView中的自定义字段上排序

时间:2014-06-25 09:51:39

标签: php sql yii cgridview

我知道已经发布了类似的问题,但我无法找到解决方案...... 我的目标是拥有一个可排序的自定义列。 (从我模型中的函数构建。) 列正确填充,标题是可点击的,但我尝试排序时有一个SQL异常。

这是我的代码:

所以我在视图中有一个CGridView:

[code]
$dataprovider = $parametre->searchByUser($user->Id_User,'validateur');

$this->widget('zii.widgets.grid.CGridView', array(
        'id'=>'ValidIndice-grid',
        'dataProvider'=>$dataprovider,
        'rowCssClassExpression'=>'$data->color',
        'selectableRows'=>1,
        'selectionChanged'=>'function(id){ location.href = "'.$this->createUrl('Parametre/view').'/id/"+$.fn.yiiGridView.getSelection(id);}', //array('idEvent'=>$evenement->Id_Evenements),
        'htmlOptions'=>array('class'=>'grid-view mgrid_table'),

        'columns'=>array(

                array(
                        'name'=>'Libelle',
                        'header'=>'Libellé du paramètre',
                        'value'=>'$data->Libelle'
                ),
                array(                      
                        'header'=>'Nouvelle donnée?',
                        'name'=>'NewValue',
                        'value'=>'$data->NewValue',                 

                        ),
));
?>
[/code]

在我的模特中:

[code]

public  function searchByUser($idUser,$role)
    {
        $criteria=new CDbCriteria;
        $criteria->alias = 'Parametre';

        $criteria->join='LEFT JOIN Droit ON Droit.Id_Parametre  = Parametre.Id_Parametre';

        $criteria->compare('Id_Parametre',$this->Id_Parametre,true);
        $criteria->compare('Libelle',$this->Libelle,true);

             ... Other criteria ... 

        $criteria->compare('NewValue',$this->NewValue);

        $sort = new CSort();
        $sort->attributes = array(
                'NewValue'=>array(
                        'asc'=>'NewValue ASC',
                        'desc'=>'NewValue DESC',
                ),
                '*', // this adds all of the other columns as sortable
        );

        return new CActiveDataProvider($this, array(
                'criteria'=>$criteria,
                'sort'=>$sort,
        ));
    }
[/code]

并在同一模型中使用我的函数来查看是否有新值:

[code]
    public function getNewValue() //mean => hasNewValue()
    {   
        $condition = 'Id_Parametre=:IdParam AND boolValide != 1';
        $params =  array(':IdParam'=>$this->Id_Parametre);
        if(Valeur::model()->exists($condition,$params)!=null)
        {
            return true;
        }   
        else 
        {
            return false;
        }
    }
[/code]

这样,我的CGridView被正确填充,并且我的" newValue"列是可点击的。 但是当我点击它时,我有一个sql例外,因为他找不到专栏&new 39" newValue'。 它的逻辑,这个专栏不存在,因为它是一个函数(getNewValue)。 怎么了?提前致谢 !

如果需要,这是错误:

  

【引用】

[code]
Error 500: <h1>CDbException</h1>
<p>CDbCommand n'a pas pu exécuter la commande SQL : SQLSTATE[42S22]: Column not found: 1054 Unknown column 'NewValue' in 'order clause'. The SQL statement executed was: SELECT `Parametre`.`Id_Parametre`, `Parametre`.`Libelle`, `Parametre`.`DateDebut`, `Parametre`.`DateFin`, `Parametre`.`Ponderation`, `Parametre`.`Frequence`, `Parametre`.`ValeurCible`, `Parametre`.`Max`, `Parametre`.`BorneVO`, `Parametre`.`BorneOR`, `Parametre`.`Min`, `Parametre`.`Unite`, `Parametre`.`boolIndicateur`, `Parametre`.`Id_Indicateur` FROM `parametre` `Parametre` LEFT JOIN Droit ON Droit.Id_Parametre  = Parametre.Id_Parametre WHERE droit.BoolEncodage = 1 AND droit.Id_User = 0 ORDER BY NewValue ASC LIMIT 10 (D:\WWW\VincentM\yii\framework\db\CDbCommand.php:543)</p><pre>#0 D:\WWW\VincentM\yii\framework\db\CDbCommand.php(396): CDbCommand->queryInternal('fetchAll', Array, Array)
#1 D:\WWW\VincentM\yii\framework\db\ar\CActiveRecord.php(1351): CDbCommand->queryAll()
#2 D:\WWW\VincentM\yii\framework\db\ar\CActiveRecord.php(1470): CActiveRecord->query(Object(CDbCriteria), true)
#3 D:\WWW\VincentM\yii\framework\web\CActiveDataProvider.php(199): CActiveRecord->findAll(Object(CDbCriteria))
#4 D:\WWW\VincentM\yii\framework\web\CDataProvider.php(168): CActiveDataProvider->fetchData()
#5 D:\WWW\VincentM\yii\framework\zii\widgets\CBaseListView.php(111): CDataProvider->getData()
#6 D:\WWW\VincentM\yii\framework\zii\widgets\grid\CGridView.php(339): CBaseListView->init()
#7 D:\WWW\VincentM\yii\framework\web\CBaseController.php(147): CGridView->init()
#8 D:\WWW\VincentM\yii\framework\web\CBaseController.php(172): CBaseController->createWidget('zii.widgets.gri...', Array)
#9 D:\WWW\VincentM\PilotTool\protected\views\site\_Encode.phtml(68): CBaseController->widget('zii.widgets.gri...', Array)
#10 D:\WWW\VincentM\yii\framework\web\CBaseController.php(126): require('D:\WWW\VincentM...')
#11 D:\WWW\VincentM\yii\framework\web\CBaseController.php(95): CBaseController->renderInternal('D:\WWW\VincentM...', Array, true)
#12 D:\WWW\VincentM\yii\framework\web\CController.php(869): CBaseController->renderFile('D:\WWW\VincentM...', Array, true)
#13 D:\WWW\VincentM\PilotTool\protected\views\site\desktop_index.phtml(13): CController->renderPartial('_Encode', Array, true)
#14 D:\WWW\VincentM\yii\framework\web\CBaseController.php(126): require('D:\WWW\VincentM...')
#15 D:\WWW\VincentM\yii\framework\web\CBaseController.php(95): CBaseController->renderInternal('D:\WWW\VincentM...', NULL, true)
#16 D:\WWW\VincentM\yii\framework\web\CController.php(869): CBaseController->renderFile('D:\WWW\VincentM...', NULL, true)
#17 D:\WWW\VincentM\yii\framework\web\CController.php(782): CController->renderPartial('desktop_index', NULL, true)
#18 D:\WWW\VincentM\PilotTool\protected\controllers\SiteController.php(66): CController->render('desktop_index')
#19 D:\WWW\VincentM\yii\framework\web\actions\CInlineAction.php(49): SiteController->actionIndex()
#20 D:\WWW\VincentM\yii\framework\web\CController.php(308): CInlineAction->runWithParams(Array)
#21 D:\WWW\VincentM\yii\framework\web\CController.php(286): CController->runAction(Object(CInlineAction))
#22 D:\WWW\VincentM\yii\framework\web\CController.php(265): CController->runActionWithFilters(Object(CInlineAction), Array)
#23 D:\WWW\VincentM\yii\framework\web\CWebApplication.php(282): CController->run('index')
#24 D:\WWW\VincentM\yii\framework\web\CWebApplication.php(141): CWebApplication->runController('site/index')
#25 D:\WWW\VincentM\yii\framework\base\CApplication.php(180): CWebApplication->processRequest()
#26 D:\WWW\VincentM\PilotTool\index.php(21): CApplication->run()
#27 {main}</pre>
[/code]
  

[/报价]

2 个答案:

答案 0 :(得分:1)

试试这个可行。

将表'Droit'中的'NewValue'列调用为CSort()中的Droit.NewValue。

$sort->attributes = array(
            'NewValue'=>array(
                    'asc'=>'Droit.NewValue ASC',
                    'desc'=>'Droit.NewValue DESC',
            ),
            '*', // this adds all of the other columns as sortable
    );

答案 1 :(得分:0)

我使用了数据表而不是CGridView&amp;数据提供器。它更直观;简单明了;

我无法理解数据提供者中存在的所有数据及其工作原理。