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

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,

  • 写回答

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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示