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条)

报告相同问题?

悬赏问题

  • ¥100 求购一套带接口实现实习自动签到打卡
  • ¥50 MacOS 使用虚拟机安装k8s
  • ¥500 亚马逊 COOKIE我如何才能实现 登录一个亚马逊账户 下发新 COOKIE ..我使用下发新COOKIE 导入ADS 指纹浏览器登录,我把账户密码 修改过后,原来下发新COOKIE 不会失效的方式
  • ¥20 玩游戏gpu和cpu利用率特别低,玩游戏卡顿
  • ¥25 oracle中的正则匹配
  • ¥15 关于#vscode#的问题:把软件卸载不会再出现蓝屏
  • ¥15 vimplus出现的错误
  • ¥15 usb无线网卡转typec口
  • ¥30 怎么使用AVL fire ESE软件自带的优化模式来优化设计Soot和NOx?
  • ¥15 Ubuntu20.04.4.LTS系统如何下载安装VirtualBox虚拟机?