Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Cgridview filter relation improvement #2007

Open
n3okill opened this Issue · 1 comment

2 participants

@n3okill

Describe the problem feature solves clearly. Explain why you need this and what is it exactly.

When we work with a cgridview where model's have relations and we need to display data from that relations, in most of the cases we need to filter and is really annoying to create new model variables and label's and set them in search().

Suggest on how it should be implemented if you have an idea.

I suggest a litle tweak in the framework/zii/widgets/grid/CDataColumn.php -> renderFilterCellContent() for the filter to handle the relation fields.

With that tweak we can use the filter's like this:

In controller

$model = new model('search');
$model->relationName = new relationModel('search');

if(isset($_GET['Model']))
    $model->attributes=$_GET['Model'];

if(isset($GET['RelationModel']))
    $model->relationName->attributes = $_GET['RelationModel'];

##In model

public function search() {
    $criteria = new CDbCriteria;

    //....
    $criteria->compare('relationName.field',$this->relationName->field,true);
    $criteria->with = array('relationName');
    $criteria->together = true;
    //....

    return new CActiveDataProvider($this, array(
        'criteria' => $criteria,
    ));
}

##In gridView

    $this->widget('zii.widgets.grid.CGridView', array(
        'dataProvider' => $model->search(),
        'filter' => $model,
        'columns' => array(
            'id',
            'field',
            'relationName.field'
        ),
));

Will open a pull request once doc block has been added using the guidelines outlined here - https://github.com/yiisoft/yii/wiki/Git-workflow-for-Yii-contributors

@suckkay

which relation type this tweak will work ?. i had trying with has_many relationship . but error

"Trying to get property of non-object"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.