我无法在网格视图中获得唯一结果。
到目前为止,我所做的是:
$query = Products::find()->select('id_product_provider')->distinct();
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSize' => 100
]
]);
结果是我想要的,但网格视图没有显示除some-column
之外的所有其他列,
输出: 我不知道,结果是我想成为的。但是网格视图没有显示所有其他所需的列,如名称,描述等。
我将查询更新为以下内容:
$query = Products::find()->select('other_columns,some_column')->distinct();
控制器代码:
$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 :(得分:0)
结果是你在select中提供的列的组合(而不仅仅是some_column)
在这种情况下$query = Products::find()->select('other_columns,some_column')->distinct();
other_columns, some_column
-
您可以使用此
检查真正执行的查询 var_dump(Products::find()->select('other_columns,some_column')->distinct()
->createCommand()->getSql());
那么你的查询应该是
$query = Products::find()->
select('name, description, price, price_category')->distinct();
但是如果你只需要一行,你可以使用聚合函数,而不是使用不同的
$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']);