简体   繁体   中英

CGridView filtering/searching relations using joining table

I know there are a million and one thread on this subject, going through them I find differing information and can't find one specific to my problem, the over abundance of threads on this subject is actually less helpful than only having a handful. Please could someone take a few minutes to help.

I have 3 tables, here they are with relevant fields

Question (primary key = id)
Tag (primary key = id, text)
Question_Tag (question_id, tag_id)

Question Model Relations

     return array(
        'tags'=>array(
            self::MANY_MANY,
            'Tags',
            'question_tag(tag_id, question_id)'
        ),
        'question_tag'=>array(
            self::HAS_MANY,
            'QuestionTag',
            'question_id',
        ),
    );
}

Tag Model relations

     return array(
        'questions'=>array(
            self::MANY_MANY,
            'Questions',
            'question_tag(question_id, tag_id)'
        ),
        'question_tag'=>array(
            self::HAS_MANY,
            'QuestionTag',
            'tag_id',
        ),
    );
}

I have a CGridView, in this grid view I would like to display Question records (which I can do), I would also like to display, all tag records associated with each Question record's, 'text' field, so "tag.text", in the same column. I also need to be able to filter these tags, it doesn't matter if the actual searching/filtering of tags is done individually for each Question_Tag record, but displayed in the Grid they should be in the same column.

I can't for the life of me, after reading every wiki page, tutorial and forum thread, figure out how to do it. I can find fragments of the solution but I can't put them together. I've only been using Yii for a few days so I guess I don't understand it all properly.

Can anyone point me in the right direction on how to handle this situation?

Thank you very much!

$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider, //Data Provider Of Quuestions
'columns'=>array(
            array(            
                        'name'=>'name',
                        'value'=>'$data->name',
            ),
            array(
                'name'=>'tags'  
                'header'=>'Tags',
                'value'=>array($this,'displayRelatedTags'),
            ),),));

Above widget will call for displayRelatedTags function to display tags, So you have write following function in your respective controller

public function displayRelatedTags($data,$row)
{
foreach($data->tags as $tag) /*$data->tags is relation which you defined as MANY TO MANY in Questions class*/
{
$arrTags[]=$tag->text;
}
return implode(',',$arrTags); /*It will display comma sepearated tags in same row*/
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM