duanhe6718
2016-02-23 10:11
浏览 87
已采纳

Yii2:在gridview中显示和过滤关系表

I have two tables: people & categories

People

- id
- name
- ...

Categories

- id
- name
- description

And another table with the relation between both as one person can have multiple categories assigned:

PeopleCategoriesAssn

- peopleID
- categoryID

I can show the categories IDs in my gridview (views/people/index.php) with:

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ...,
        [
            'label' => 'Category',
            'value' => function ($data) {
                $output = '';
                foreach($data->peopleCategoriesAssns as $request) {
                    $output .= $request->talentCategoryID.'<br>';
                }
                return $output;
            },
        ],
        ...

which uses this from models/People.php

public function getPeopleCategoriesAssns() 
{ 
    return $this->hasMany(PeopleCategoriesAssn::className(), ['peopleID' => 'id']); 
} 

How could I show the categories names instead of the categoies IDs within the gridview and also, what should i do at /models/PeopleSearch.php in order to allow search/filter on that field?

Thank you in advance,

图片转代码服务由CSDN问答提供 功能建议

我有两张桌子:人和&amp; 类别

   -  id 
- name 
- ... 
   
 
 

类别

   -  id 
- name 
- description 
   
 
 

另一个表与两个人之间的关系可以分配多个类别:

PeopleCategoriesAssn

   -  peopleID 
- categoryID 
   
 
 

我可以在gridview中显示类别ID(views / people / index .php)with:

 &lt;?= GridView :: widget([
'dataProvider'=&gt; $ dataProvider,
'filterModel'=&gt; $ searchModel  ,
'列'=&gt; [
 ...,
 [
'标签'=&gt;'类别',
'值'=&gt;函数($ data){
 $ output =  ''; 
 foreach($ data-&gt; peopleCategoriesAssns as $ request){
 $ output。= $ request-&gt; talentCategoryID。'&lt; br&gt;'; 
} 
返回$ output; 
}  ,
  ],
 ... 
   
 
 

使用了models / People.php

 公共函数getPeopleCategoriesAssns  ()
 {
返回$ this-&gt; hasMany(PeopleCategoriesAssn :: className(),['peopleID'=&gt;  'ID']);  
} 
   
 
 

如何在gridview中显示类别名称而不是categoies ID,以及我应该在/models/PeopleSearch.php中做什么 为了允许在该字段上搜索/过滤?

提前感谢您,

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongzhi8984 2016-02-23 10:45
    已采纳

    There are two parts involved with your problem.

    1. Showing the values

    This is just a respecification of your GridView-column:

    'columns'=>[
        //...
        [
            'attribute'=>'catNameSearch',
            'value'=>function ($model, $key, $index, $column) {
                $cats = $model->peopleCategoriesAssns;
                if (empty($cats)) {
                    return null;
                } else {
                    return implode(', ', ArrayHelper::getColumn($cats, 'name');
                }
            },
        ],
        //...
    ]
    

    2. Searching cat names

    Now to solve the problem of searching them it gets a little more complex. As you can see I set the attribute above to catNameSearch. You need to add this as a public var to your PeopleSearch-class and declare it as safe in a validator of the search-class.

    class PeopleSearch extends \app\models\People
    {
    
        public $catNameSearch;
    
        public function rules()
        {
            return [
                //...
                [['catNameSearch'], 'safe'],
                //...
            ];
        }
     }
    

    In the search-method itself you add a sub-query to show only people assigned to the corresponding category. This could be done like so:

    if (!empty($this->catNameSearch)) {
        $peopleIdsMatching = PeopleCategoriesAssn::find()
             ->select('peopleID')
             ->distinct(true)
             ->joinWidth('category')
             ->andWhere(['like', ['category.name'=>$this->catNameSearch]])
            ->column();
    
         $this->andWhere(['id'=>$peopleIdsMatching]);
    }
    

    Now only people with a relation to the given category will be shown. This could be further optimized with performing the comparison in an actual sub-query of the people-search and not doing a seperate query, but this should give you the basic idea!

    点赞 打赏 评论
  • dongxing9219 2016-02-23 10:39

    To answer the first part you need to add another method to the People class:

    public function getCategories() {
        return $this->hasMany( Categories::className(), [ 'id' => 'categoryID' ] )
                    ->via('PeopleCategoriesAssn');
    }
    

    See: http://www.yiiframework.com/doc-2.0/yii-db-activerelationtrait.html#via()-detail

    This will allow you to do:

    foreach( $data->categories as $category) {
        $output .= $category->name.'<br>';
    }
    

    As for searching, I'm not sure, but adding public $categoryName property to the search class would be a good starting point.

    点赞 打赏 评论

相关推荐 更多相似问题