M
M
Mikha Pankratov2016-09-08 09:29:19
Yii
Mikha Pankratov, 2016-09-08 09:29:19

How to make a connected multi-sort in ActiveDataProvider?

Good afternoon,
sorting by additional table in ActiveDataProvider does not work

$query = User::find()
->leftJoin(Table::tableName().' p','user.id = p.user_id');
        $this->processPageRequest('page');
        $provider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'pageSize' => 3,
                'page' =>\Yii::$app->request->get('page'),
            ],
        ]);

        $provider->setSort([
            'defaultOrder' => [],
            'attributes' => [
                'table.xx1' => \Yii::$app->request->post('xx1') == 3 ? SORT_ASC: SORT_DESC,
                'table.xx2' => \Yii::$app->request->post('xx2') == 3 ? SORT_ASC: SORT_DESC,
                'table.xx3' => \Yii::$app->request->post('xx3') == 3 ? SORT_ASC: SORT_DESC,
            ]
        ]);

here are my attempts) I don’t understand how and where to prescribe something so that it works

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2016-09-08
@webinar

Where is join?
with is eager loading. Loading a connection hasMany in one request. And it happens after the main request.
You need to add joinWith www.yiiframework.com/doc-2.0/guide-db-active-recor...
Next:

$dataProvider->sort->attributes['xx1'] = [
        'asc' => ['table.xx1' => SORT_ASC],
        'desc' => ['table.xx1' => SORT_DESC],
    ];

Watch here: www.yiiframework.com/wiki/653/displaying-sorting-a...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question