如何在yii2中连接3个表并在Gridview中显示,然后使排序正常工作

时间:2015-04-08 02:56:30

标签: yii2

我使用gii工具创建了crud应用程序。我有3个表tbl_targetcities,lib_cities和lib_provinces。我能够将lib_cities连接到tbl_targetciteis而不是lib_provinces。而且城市/市政的分类也不起作用。它似乎根据身份证进行排序。

tbl_target_cities

enter image description here

lib_cities

enter image description here

lib_provinces

enter image description here

示例视图

enter image description here

到目前为止,这是我在模型中的关系。

public function getCityName()
{
  return $this->hasOne(LibCities::className(),['city_code'=>'city_code']);
}

在我的视图文件中......

    <?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        [
            'attribute'=>'city_code',
            'value'=>'cityName.city_name'
        ],
                   [
            'attribute'=>'prov code',
            'value'=>'cityName.city_name'
        ],
        'kc_classification',
        'cluster',
        'grouping',
         'priority',
        'launch_year',

        ['class' => 'yii\grid\ActionColumn'],
    ],
]); ?>

如何从lib_provinces ???中显示prov_name

编辑以在评论框中回答用户2839376问题

在搜索模型类中

$query = TblSpBub::find();
    $query->joinWith('brgyCode')->joinWith(['cityCode'])->joinWith(['cityCode.provCode']);

    $covered=  LibAreas::find()->where(['user_id'=>yii::$app->user->identity->id])->all();

    $query->all();

    $dataProvider = new ActiveDataProvider([
        'query' => $query,
        'sort'=> ['defaultOrder' => ['id'=>SORT_DESC]],
    ]);

    $dataProvider->sort->attributes['city'] = [
    'asc' => ['lib_Cities.city_name' => SORT_ASC],
    'desc' => ['lib_Cities.city_name' => SORT_DESC],
    ];

    $dataProvider->sort->attributes['province'] = [
    'asc' => ['lib_provinces.prov_name' => SORT_ASC],
    'desc' => ['lib_provinces.prov_name' => SORT_DESC],
    ];

3 个答案:

答案 0 :(得分:6)

LibCities模型中添加新关系:

public function getProvince()
{
  return $this->hasOne(LibProvince::className(),['prov_code'=>'prov_code']);
}

并改变getCityName关系。您应该为关系添加with()

public function getCityName()
{
  return $this->hasOne(LibCities::className(),['city_code'=>'city_code'])->with(['province']);
}

并且在视图中更正了你的columnto:

 [
            'attribute'=>'prov code',
            'value'=>'cityName.province.prov_name'
        ],

答案 1 :(得分:0)

你必须在模型中使用函数relations()。

在tbl_target_cities模型中:

public function relations()
  {
    return array(
      'city' => array(self::HAS_ONE, 'LibCities', 'city_code'),
    );
  }

在LibCities模型中:

public function relations()
  {
    return array(
      'province' => array(self::HAS_ONE, 'LibProvinces', 'prov_code'),
      'targets' => array(self::HAS_MANY, 'TargetCity', 'city_code',
    );
  }

这将允许你跳出LibCities模型, 现在你可以简单地访问这样的名字:

$model->city->province->prov name;

注意:您需要定义3个模型。

修改

array(
  'name' => 'province name',
  'value' => $data->city->province->prov_name;
),

答案 2 :(得分:0)

完成它,Heres如何。

除上述代码(原帖)

//在Model I中添加了一个附加功能

public function getTaskowner()
{
   return $this->hasOne(Tasks::className(), ['id' => 'task_id'])
       ->with(
           ['location','taskowner']
       );
}

and in view i did this

 ....
'columns' => [
 ....
[
  'class' => 'kartik\grid\DataColumn',
  'value'=> 'tasks.location.taskowner.name',
  .....
],
.....

并且有效

关键点。使用带有&#39; with-&gt;(..)&#39;的数组。然后在视图中添加两个“tasks.location.taskowner.name&#39;”,将它们全部加入