Yii2排序间接相关的列,它不是gridview模型的一部分

时间:2015-01-26 12:51:08

标签: gridview yii

我有一个显示公司数据的gridview。每家公司都有tehsil_id作为外键。下面是从父级到子级的表结构:

Country->province->city->district->tehsil

每家公司都位于特定的公司。可以使用tehsil ID向后追踪地区或城市或省份。

我面临的问题是,我想列出的公司不仅可以按Tehsil ID(公司表中的列)进行排序,还可以按城市ID /省ID /国家ID(这些ID都不是其中的一部分)排序公司表)。

我能够在gridview中列出这些相关的区/城市/省,但无法使用这些值进行排序。我们将不胜感激。

<?php
echo GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [

        'id',
        'name',
        // generated by schmunk42\giiant\crud\providers\RelationProvider::columnFormat
        [
            "class" => yii\grid\DataColumn::className(),
            "attribute" => "tehsil_id",
            'filter' => ArrayHelper::map(Tehsil::find()->orderBy('name')->asArray()->all(), 'id', 'name'),
            "value" => function ($model) {
                if ($rel = $model->getTehsil()->one()) {
                    return yii\helpers\Html::a($rel->name, ["crud/tehsil/view", 'id' => $rel->id,], ["data-pjax" => 0]);
                } else {
                    return '';
                }
            },
            "format" => "raw",
        ],

        [
            "class" => yii\grid\DataColumn::className(),
            "attribute" => "district_id",
            'filter' => ArrayHelper::map(District::find()->orderBy('name')->asArray()->all(), 'id', 'name'),
            "value" => function ($model) {
                if ($rel = $model->getTehsil()->one()->getDistrict()->one()) {
                    return yii\helpers\Html::a($rel->name, ["crud/district/view", 'id' => $rel->id,], ["data-pjax" => 0]);
                } else {
                    return '';
                }
            },
            "format" => "raw",
        ],


        [
            "class" => yii\grid\DataColumn::className(),
            "attribute" => "city_id",
            'filter' => ArrayHelper::map(City::find()->orderBy('name')->asArray()->all(), 'id', 'name'),
            "value" => function ($model) {
                if ($rel = $model->getTehsil()->one()->getDistrict()->one()->getCity()->one()) {
                    return yii\helpers\Html::a($rel->name, ["crud/city/view", 'id' => $rel->id,], ["data-pjax" => 0]);
                } else {
                    return '';
                }
            },
            "format" => "raw",
        ],
        // generated by schmunk42\giiant\crud\providers\RelationProvider::columnFormat
        [
            "class" => yii\grid\DataColumn::className(),
            "attribute" => "company_id",
            "value" => function ($model) {
                if ($rel = $model->getCompany()->one()) {
                    return yii\helpers\Html::a($rel->name, ["crud/company/view", 'id' => $rel->id,], ["data-pjax" => 0]);
                } else {
                    return '';
                }
            },
            "format" => "raw",
        ],
        'phone',
        'fax',
        'mobile',
        'email:email',
        /*
        'created_on',
        'updated_on',
        'created_by',
        'updated_by',
        */
        [
            'class' => 'yii\grid\ActionColumn',
            'urlCreator' => function ($action, $model, $key, $index) {
                // using the column name as key, not mapping to 'id' like the standard generator
                $params = is_array($key) ? $key : [$model->primaryKey()[0] => (string)$key];
                $params[0] = \Yii::$app->controller->id ? \Yii::$app->controller->id . '/' . $action : $action;
                return \yii\helpers\Url::toRoute($params);
            },
            'contentOptions' => ['nowrap' => 'nowrap']
        ],
    ],
]);

1 个答案:

答案 0 :(得分:0)

您只能对原始查询中存在的列进行排序。您很可能拥有CompanySearch类,其search()方法。在其中,将更多表连接到原始查询:

$query = Company::find()->joinWith([
    'tehsil',
    'tehsil.district',
    'tehsil.district.city',
    'tehsil.district.city.province',
    'tehsil.district.city.province.country'
]);

然后将新列添加到网格:

echo GridView::widget([
    ...
    'columns' => [
        'id',
        'name',
        'tehsil',
        'tehsil.district',
        'tehsil.district.city',
        'tehsil.district.city.province',
        'tehsil.district.city.province.country',
        ...

它们应该是可排序的。

另外,请查看本教程:Kivy rule inherence with add_widget()