I’ve had good success setting up filtering on lots my models from this article.
However, problem I’ve run into is filtering on a column of values returned by a many-to-many relationship in some of my database tables. I have a ‘service’ database table, which consists of singular entities and group entities. The table has a many-to-many relationship to itself, linking group entities to their members and vice-versa. I tried following the comments here, but without success. When I do so, attempting to filter on the Member column on returns lines matching on the service_name column.
Any clues or assistance would be much appreciated .
Service model:
public function relations()
{
return array(
'childServiceGrps' => array(self::MANY_MANY, 'Service', 'service_contains_service(service_service_id, service_service_id1)'),
'parentServiceGrps' => array(self::MANY_MANY, 'Service', 'service_contains_service(service_service_id1, service_service_id)'),
);
}
public function getMembers() {
return implode( '<br/>', CHtml::listData( $this->childServiceGrps,'service_id','service_name' ) );
}
public function searchGrp()
{
$criteria=new CDbCriteria;
$criteria->compare('t.service_name',$this->service_name,true);
$criteria->compare('t.service_type','group',true);
if( !empty( $this->member_filter ) ) {
$criteria->addCondition( 't.service_id IN (SELECT childServiceGrps.service_id FROM service childServiceGrps WHERE childServiceGrps.service_name LIKE :relNameLike)' );
$criteria->params[':relNameLike'] = '%' . $this->member_filter . '%';
}
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'pagination'=>array(
'pageSize'=>20,
),
));
}
Service Group view:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$model->searchGrp(),
'filter'=>$model,
'columns'=>array(
'service_name',
array(
'name'=>'member_filter',
'header'=>'Members',
'type'=>'html',
'value'=>'$data->members',
),
'comments',
),
)); ?>