Hi,
I need some help - I’m new in Yii…
I’ve tried create a CDbCriteria, because i’ve would like to got back special data result from DB. So i’ve made a new method in my model.
My problem is that the result of the query is two AS (alias) record, not a normal field in the DB!
I’ve solved this problem - added with two public variable to the Class.
But afther that i cant sort an filter the result in view (CGridView). The title of the grid ain’t anchor, so i can’t click on it.
Addictionally the “filter” doen’t work
Can anyone give me a hint - how to solve my big problem?
By the way - Is there any much normal case to use CDbCriteria and CGridView, when i have to take a special query (Eg. a lots of alias field)
Thanks advance!!
MODEL:
...
public $searchYear;
public $searchAmount;
...
public function practice()
{
$criteria = new CDbCriteria;
$criteria->compare('myDate',$this->myDate,true); // normal DB field
$criteria->compare('myAmount',$this->myAmount); // normal DB field
$criteria->compare('searchYear',$this->searchYear); // JUST AN ALIAS
$criteria->compare('searchAmount',$this->searchAmount); // JUST AN ALIAS
$criteria->select = array(
'DATE_FORMAT(myDate,\'%Y\') as searchYear, sum(myAmount) AS searchAmount'
);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
++++
CONTROLLER:
public function actionMypractice()
{
$modelSearch = new Practice('practice');
$modelSearch->unsetAttributes();
if(isset($_GET['Practice']))
$modelSearch->attributes=$_GET['Practice'];
$this->render('index',array(
'modelSearch'=>$modelSearch,
));
}
++++
VIEW:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'practice-grid',
'dataProvider'=>$modelSearch->practice(),
'filter'=>$modelSearch,
'columns' => array(
'searchYear',
'searchAmount',
),
));