frocco
(Farocco)
November 19, 2010, 3:13pm
1
Hello,
I added a joined field cat.cat_name to the cgridview and cannot search on it.
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'product-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'pd_id',
'cat.cat_name',
derelict
(Just Worker)
November 19, 2010, 3:20pm
2
You need to configure sort property of CActiveDataProvider in search method of your model.
UPD: I read not inattentively and think you need sort not search.
danaluther
(Dana Luther)
November 19, 2010, 4:19pm
4
You need to update the search method of the model to include the join:
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->join = 'LEFT JOIN related_tbl as tbl_alias ON tbl_alias.id = fk_id';
$criteria->compare('id',$this->id);
//... standard criteria here
$criteria->compare(' tbl_alias.searchable1',$this->tbl_alias->searchable1, true);
// etc. add the other fields of the relation tbl_alias that you want to access
return new CActiveDataProvider('MyModelName', array(
'criteria'=>$criteria,
));
}
This works well for me. There may be a more elegant solution to the join.
frocco
(Farocco)
November 19, 2010, 4:38pm
5
Thanks,
I need to do another join, even through I have it joined in relations?
jacmoe
(Jacob Moen)
November 19, 2010, 4:45pm
6
Nope.
In your models ‘search’ function you probably have a list of criteria compares, like this:
$criteria->compare('id', $this->id);
If you want to search by a related models field, then include that in the list of criteria:
$criteria->compare('cat_name', $this->cat_id);
To make that work, put this before the compare conditions:
$criteria->with = array('cat');
‘with’ is just an elegant way of declaring a join.
You need to include all related tables, of course.
And you might need to prefix some of the field names due to ambuigity.
frocco
(Farocco)
November 19, 2010, 4:57pm
7
Still not working. Here is my code.
public $search_location;
public function relations()
'cat' => array(self::BELONGS_TO, 'Category', 'cat_id'),
$criteria->with = array('cat');
$criteria->compare('cat_name', $this->search_location);
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'product-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'pd_id',
array(
'name'=>'search_location',
'value'=>'$data->cat->cat_name',
'header'=>'Category',
),
frocco
(Farocco)
November 19, 2010, 5:00pm
8
Thank you all, It is working now.
I had to add the true word.
I used jacmoe solution.
$criteria->compare('cat_name', $this->search_location, true);
jacmoe
(Jacob Moen)
November 19, 2010, 5:04pm
9
<edit> I saw that you fixed it - great! </edit>
frocco
(Farocco)
November 19, 2010, 5:09pm
10
I really appreciate the help.
I am coming from a codeigniter background.
I’m coming from CI too, your among friends here!
doodle