A
A
akula222017-05-29 17:50:04
MySQL
akula22, 2017-05-29 17:50:04

How to make a group limit in yii2 ArrayDataProvider?

Good day.
There is a database
1 group=1
2 group=1
3 group=2
4 group=2
I need to display only 1 record from each group in the grid.
Now the whole thing is displayed with such a query

$query = TourTable::find()
            ->with(['user', 'profile', 'team'])
            ->where(['tour_id' => $tour_id]);

        $dataProvider = new ArrayDataProvider([
            'allModels' => $query->all(),
            'pagination' => false,
            'sort' => [
                'defaultOrder' => ['group' => SORT_ASC, 'total' => '', 'difference' => ''],
                'attributes' => [
                    'group',
                    'total',
                 
                    'difference',
                ],
            ],
        ]);

Can this be done in the query, if not, can you already remove the extra ones in the grid itself?
Tell me how

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Boris Korobkov, 2017-05-29
@BorisKorobkov

$query = TourTable::find()
            ->with(['user', 'profile', 'team'])
            ->where(['tour_id' => $tour_id])
            ->groupBy('group');

But keep in mind that when grouping, you can use either grouping fields or aggregate functions from other fields. For more details see https://dev.mysql.com/doc/refman/5.7/en/group-by-f...
If it doesn't matter which value to choose (when there are several in a group), ANY can be used

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question