2016-11-10 4 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

결과는 당신이 other_columns, some_column에 의해이 경우 $query = Products::find()->select('other_columns,some_column')->distinct();에서 선택 (및뿐만 아니라 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(); 

하지만 당신은 단지 행을해야 할 경우 당신은 별개의

$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 필요하지만, 그리드보기에서 모든 열을 표시하지 않음으로써 집계 함수 및 그룹을 사용 shuld. 그게 나타나지 않아. – TNC

+0

나는 이해하지 못한다. 적절한 데이터 샘플 e와 예상되는 질문으로 업데이트해야한다. 결과 .. – scaisEdge

+0

격자보기가 다른 모든 열을 표시하지는 않지만 식별 열만 표시합니다. – TNC