Hi there,
I am setting up an admin backend based on the standard MVC structure generated by gii.
The default view generated by gii associated with the "admin" action has a very useful table with an AJAX filtering built in.
My Article db table has a category_id column which is related to the category table where I can retrieve the category name.
I have customized the admin table of my Article model so it shows the category name instead of the category id, but the search field disappeared from the first row. I guess I have to create the right CDbCriteria entry in order for it to process the query, but I don’t know how.
My Article model file looks like this:
public function rules() {
return array(
[...]
array('category_id, status, title, teaser, content, author_id, create_time, update_time', 'safe', 'on'=>'search'),
);
}
[...]
public function relations() {
return array(
'Category' => array(self::BELONGS_TO, 'Category', 'category_id'),
[...]
);
}
[...]
public function search() {
$criteria=new CDbCriteria;
$cat = $cats[$this->article_type_id];
$criteria->compare('Category.name', $this->Category->name, true); // THROWS AN EXCEPTION
$criteria->compare('status', $this->status);
$criteria->compare('title',$this->title,true);
$criteria->compare('teaser',$this->teaser,true);
$criteria->compare('content',$this->content,true);
$criteria->compare('author_id',$this->author_id,true);
$criteria->compare('create_time',$this->create_time,true);
$criteria->compare('update_time',$this->update_time,true);
return new CActiveDataProvider(get_class($this), array(
'criteria' => $criteria,
));
}
My view file looks like this:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'article-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'Category.name:text:'. Yii::t('app', 'Category'),
'status',
'title',
'author.full_name:text:'. Yii::t('app', 'Author'),
array(
'class'=>'CButtonColumn',
),
),
));
Actually, the Category.name method works in the view widget and shows the category name. But the filtering is not activated.
How can I attach my related Category model to the search function?
Thanks in advance!
gm