简体   繁体   English

使用联接表的CGridView过滤/搜索关系

[英]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. 我知道这个主题有100万个线程,通过它们我发现了不同的信息,找不到一个特定于我的问题的信息,这个主题上过多的线程实际上比只有少数几个有用。 Please could someone take a few minutes to help. 请有人花几分钟时间帮助您。

I have 3 tables, here they are with relevant fields 我有3张桌子,这里有相关领域

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. 我有一个CGridView,在此网格视图中,我想显示问题记录(我可以这样做),我也想显示与每个问题记录的“文本”字段关联的所有标签记录,因此“ tag.text” ,在同一列中。 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. 我还需要能够过滤这些标签,对于每个Question_Tag记录是否单独完成标签的实际搜索/过滤都没有关系,但是在网格中显示时,它们应该在同一列中。

I can't for the life of me, after reading every wiki page, tutorial and forum thread, figure out how to do it. 阅读完每个Wiki页面,教程和论坛主题后,我就无法终生,找出解决方法。 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. 我只使用Yii几天了,所以我想我不太了解这一切。

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 上面的小部件将调用displayRelatedTags函数来显示标签,因此您已在各自的控制器中编写了following函数

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*/
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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