【问题标题】:Sort on a custom field in a yii CGridView对 yii CGridView 中的自定义字段进行排序
【发布时间】:2014-06-25 09:51:39
【问题描述】:

我知道已经发布了类似的问题,但我找不到我的解决方案... 我的目标是有一个可排序的自定义列。 (从我的模型中的一个函数构建。) 该列已正确填充,标题是可点击的,但是当我尝试排序时出现 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 异常,因为他没有找到“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]

[/quote]

【问题讨论】:

  • 您不能对未存储在数据库中的属性进行排序,也许您可​​以在该属性上使用某种 sql 表达式
  • 您想要的是 - 带有返回 truefalse 的子查询的列。然后单击CGridView 中的链接,结果应相应排序。它更像SELECT *, (SELECT ...) as NewValue FROM ..。您确实需要 public $NewValue; 作为 Parametre 模型类中的属性。
  • 请检查Yii列中SubQuery的使用情况。

标签: php sql yii cgridview


【解决方案1】:

试试这个可能会奏效。

在您的 CSort() 中将表“Droit”中的“NewValue”列称为 Droit.NewValue。

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

【讨论】:

  • 不工作。表格Droit用于查看用户是否有权查看表格参数。但是 NewValue 函数用于查看参数是否有另一个表“Valeur”的新记录。我尝试在通话前添加 Droit 或 Parametre,但没有成功。但是,如果 sql 尝试在我的 mysql DB 中查找字段“NewValue”,它将永远找不到它,因为它不存在于 DB 中。NewValue 仅存在于我的 Parametre 模型类中。
【解决方案2】:

我使用数据表而不是 CGridView 和数据提供程序。它真的更直观;简单明了;

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

【讨论】:

    猜你喜欢
    • 2011-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多