dousenjue3214
dousenjue3214
2015-08-22 12:19

如何在yii 2中使用搜索和过滤获取外键值而不是网格视图中的键?

已采纳

I have two tables staff with columns id, name and attendance. staff_id is used as foreign key in attendance table.

I want to display staff name in attendance gridview.

Attendance model:

public function getStaff()
{
        return $this->hasOne(Staff::className(), ['id' => 'staff_id']);
}

public function getStaffName() {
          return $this->staff->name;
}

and in index.php I used this code

     <?= GridView::widget([
            [
             'attribute'=>'staff_id',
            'value'=>'StaffName',
            ],
]); ?>

to get value of staff name. In this way I am getting staff name successfully but the problem is that when I make search for staff name in gridview it say "staff_id" should be integer as I define it as integer, but here I want to search name of staff instead of id.

How is this possible ? Thanks in advance

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • dshgdhdfcas30210 dshgdhdfcas30210 6年前

    Add this in search model

    $query->joinWith(['staff(relation name)']);

    And add below code at filter query.

    $query->andFilterWhere(['like', 'staff.name', $this->staff_id])

    In staff.name that in staff is table name.

    点赞 评论 复制链接分享
  • douao1579 douao1579 4年前
    You can use this code
    
    1. relation in models
     public function getCountry()
        {
            return $this->hasOne(Country::className(), ['id' => 'country_id']);
        }
    2. in grid view
    <?= GridView::widget([
            'dataProvider' => $dataProvider,
            'filterModel' => $searchModel,
            'columns' => [
                ['class' => 'yii\grid\SerialColumn'],
    
                'id',
                'state_name',
               // 'country_id',
             [
          'attribute'=>'country_id',
          'value'=>'country.country_name',
        ],
                ['class' => 'yii\grid\ActionColumn'],
            ],
        ]); ?>
    3. Search Models
       change country_id from integer to safe than change on search function
    
     public function search($params)
        {
            $query = State::find();
            $query->joinWith(['country']);
           $dataProvider = new ActiveDataProvider([
                'query' => $query,
            ]);
            $this->load($params);
            if (!$this->validate()) {
                return $dataProvider;
            }
            $query->andFilterWhere([
                'id' => $this->id,
            ]);
            $query->andFilterWhere(['like', 'state_name', $this->state_name])
             ->andFilterWhere(['like', 'country.country_name', $this->country_id]);
    return $dataProvider;
        }
    
    点赞 评论 复制链接分享
  • dongxiaoxiao1983 dongxiaoxiao1983 6年前

    You can use this code

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'columns' => [
            'staff.name',
        ],
    ]); ?>
    

    OR use this code

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'columns' => [
            [
               'attribute' => 'staff.name',
               'header' => 'Staff title'
               'value'=> 'function ($model, $key, $index, $grid){
                   return $model->staff->name;
               }'
            ],
        ],
    ]); ?>
    

    OR in your code you can use this

    <?= GridView::widget([
        [
          'attribute'=>'staff_id',
          'value'=>'getStaffName()',
        ],
    ]); ?>
    

    and for search you can watch this video Searching Related Table Data

    点赞 评论 复制链接分享

相关推荐