I know similar questions has been already posted but I can't find my solution... My goal is to have a sortable custom column. (build from a function in my model.) The column is correctly filled, the header is clickable but I have an sql exception when I try to sort.
here is my code :
So I have a CGridView in my view :
[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]
In my model :
[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]
and in the same model My function to see if I have new value :
[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]
Whith this, my CGridView is correctly filled, and the header of my "newValue" column is clickable. But when I click on it I have an sql exception because he don't find the column 'newValue'. It's Logic, this column doesn't exist because it is a fonction (getNewValue) .. what's wrong? thanks in advance !
here is the error if it is needed :
[quote]
[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]
I used datatable instead CGridView & dataprovider. It's really more intuitive; simple and clear;
I can't understand all data present in the dataprovider and how it works.