Cgridview filter relation improvement #2007

Closed
n3okill opened this Issue Jan 18, 2013 · 1 comment

Comments

3 participants
Contributor

n3okill commented Jan 18, 2013

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

@n3okill n3okill added a commit to n3okill/yii that referenced this issue Jan 18, 2013

@n3okill n3okill #2007 - Improvement of filter with relation in Cgridview 3e4d7bb

suckkay commented May 14, 2013

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

"Trying to get property of non-object"

samdark closed this Nov 17, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment