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,
));
}
and in the same model My function to see if I have new value :
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;
}
}
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) …
‘NewValue’ is not a column in your table, so you can not use ‘compare’ on it in the ‘search()’ method.
So inside the ‘search()’ method, you have to test if ‘newValue’ is set and if it is, adap the search to a search with values in the SQL table.
For instance, I have a table with a field ‘device_identifier’ which is a complex serial number. I represent it to the user in a more readable way and I call this virtual attribute ‘textualIdentifier’. So when ‘textualIdentifier’ is set during the search, I add the appropriate SQL comparison:
You can see that in ‘$sort’, the sort order for ‘textualIdentifier’ is set as a sort on ‘device_identifier’.
For info, I have the following getter and setter for ‘textualIdentifer’ which is also declared as a ‘safe’ attribute.
/*
* Internal shadow property for textualIdentifier in case of search scenario.
*/
private $_textualIdentifier;
/**
* Setter for the textualIdentifier usefull in search scenario
*/
public function setTextualIdentifier($value){
if($this->getScenario()!=='search') {
$this->device_identifier=$this->convertToInternalIdentifier($value, $this->device_type_id);
$this->_textualIdentifier=$value;
} else {
$this->_textualIdentifier=$value;
}
}
/**
* Provides the identifier in a Human Readable form.
*
* @return $identifier
*/
public function getTextualIdentifier(){
if($this->getScenario()!=='search') {
return self::convertToTextualIdentifier($this->device_identifier,$this->device_type_id);
} else {
return $this->_textualIdentifier;
}
}
You can see that this code makes a difference between the ‘search’ scenario and other scenarios. I can also use this field to set the serial number from the human readable format because in the ‘setter’ I make sure that the ‘device_identifier’ field is updated when the search scenario is not active.