2016-11-10 60 views
0

我无法在网格视图中获得唯一结果。Yii2 gridview中的不同行

我迄今所做的是:

$query = Products::find()->select('id_product_provider')->distinct(); 
$dataProvider = new ActiveDataProvider([ 
     'query'  => $query, 
     'pagination' => [ 
      'pageSize' => 100 
     ] 
    ]); 

结果就是我想要的东西,但网格视图没有显示所有其他列比some-column

输出: enter image description here 我不知道,结果是我想成为的。但是,网格视图没有显示所需的所有其他colums喜欢的名称,说明等

我更新查询如下:

$query = Products::find()->select('other_columns,some_column')->distinct(); 

结果不是由some_column独特。 enter image description here

控制器代码:

$searchModel = new ProductsSearch(); 
     $dataProvider = $searchModel->search(Yii::$app->request->queryParams); 

     return $this->render('index', [ 
      'searchModel' => $searchModel, 
      'dataProvider' => $dataProvider, 
     ]); 

而且观点是:

GridView::widget(['dataProvider' => $dataProvider, 
           'filterModel' => $searchModel, 
           'columns'  => [['class' => 'yii\grid\SerialColumn'], 
                ['attribute' => 'Image', 
                'format' => 'html', 
                'value'  => function ($data) { 
                 return Html::img($data->image, ['width' => '100']); 
                },], 
                ['attribute'  => 'name', 
                'format'   => 'raw', 
                'value'   => function ($data) { 
                 return strlen($data->name) > 25 ? 
                  html_entity_decode(substr($data->name, 0, 25) . '...') : 
                  html_entity_decode($data->name); 
                }, 
                'contentOptions' => ['style' => 'max-width: 200px;']], 
                ['attribute'  => 'description', 
                'format'   => 'raw', 
                'value'   => function ($data) { 
                 return strlen($data->description) > 25 ? 
                  html_entity_decode(substr($data->description, 0, 25) . '...') : 
                  html_entity_decode($data->description); 
                }, 
                'contentOptions' => ['style' => 'max-width: 200px;']], 
                ['attribute' => 'price', 
                'format' => 'text', 
                'value'  => function ($data) { 
                 return html_entity_decode($data->price); 
                },], 

                ['attribute'  => 'price_category', 
                'format'   => 'text', 
                'value'   => function ($data) { 
                 return strip_tags(html_entity_decode($data->price_category)); 
                }, 
                'contentOptions' => ['style' => 'max-width: 100px;']], 
                ['attribute'  => 'product_category', 
                'format'   => 'text', 
                'filter'   => $categories, 
                'value'   => function ($data) { 
                 return strlen($data->product_category) > 25 ? 
                  html_entity_decode(substr($data->product_category, 0, 25) . '...') : 
                  html_entity_decode($data->product_category); 
                }, 
                'contentOptions' => ['style' => 'max-width: 150px;']], 
                ['attribute'  => 'provider', 
                'format'   => 'text', 
                'value'   => function ($data) { 
                 return strlen($data->provider) > 25 ? 
                  html_entity_decode(substr($data->provider, 0, 25) . '...') : 
                  html_entity_decode($data->provider); 
                }, 
                'contentOptions' => ['style' => 'max-width: 150px;'], 
                'filter'   => $providers,], 
                ['attribute'  => 'universe', 
                'format'   => 'text', 
                'contentOptions' => ['style' => 'max-width: 100px;'], 
                'filter'   => ['fabrics' => 'fabrics', 'wool' => 'wool', 'paper' => 'paper'],], 
                'id_product_provider', 
                ['class' => 'yii\grid\ActionColumn', 
                'header' => 'Action', 
                'template' => '{info}   {detail}', 
                'buttons' => ['info' => function ($url, $model) { 
                 return Html::a('<span class="glyphicon glyphicon glyphicon-eye-open"></span>', $model->url, ['title' => Yii::t('app', 'Info'), 
                                        'target' => '_blank']); 
                }, 
                    'detail' => function ($url, $model) { 
                     $url = str_replace(' ', '-', $model->universe) . '/' . str_replace(' ', '-', $model->product_category) . '/' . $model->slug . '/' . $model->id; 
                     $url = Yii::$app->urlManagerFrontEnd->createUrl($url); 


                     return Html::a('<span class="glyphicon glyphicon glyphicon glyphicon-picture"></span>', $url, ['title' => Yii::t('app', 'Info'), 
                                            'target' => '_blank']); 
                    }],]],]); 
     ?> 

任何帮助将不胜感激。

回答

0

结果由你选择(而不是只由some_column)

在这种情况下$query = Products::find()->select('other_columns,some_column')->distinct();other_columns, some_column提供列的组合distinc -

您可以使用此

检查真正执行查询
var_dump(Products::find()->select('other_columns,some_column')->distinct() 
     ->createCommand()->getSql()); 

然后将查询应该是

$query = Products::find()-> 
    select('name, description, price, price_category')->distinct(); 

但如果你只需要一个行你shuld通过不明显

$query = Products::find()-> 
    select('max(name) as name, 
      max(description) as description, 
      max(price) as price, 
      max(price_category) as price_category ')->groupBy(['id_product_provider']); 
+0

是啊,我只需要通过只有一列uniness,但在网格视图中显示的所有列使用聚合函数和组。它没有出现。 – TNC

+0

我不明白..你应该用适当的数据样本e和预期更新你的问题。结果.. – scaisEdge

+0

网格视图不显示所有其他列,但仅显示清晰度列。 – TNC