M
M
MaikMain2017-06-22 17:13:37
Yii
MaikMain, 2017-06-22 17:13:37

Why doesn't searchModel.Yii2 work?

Good afternoon, if I write like this:

public function actionIndex()
    {
        $searchModel = new OrdersSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        $dataProvider = new ActiveDataProvider([
            'query' => Orders::find(),
            'pagination' => [
                'pageSize' => 10,
                'forcePageParam' => false,
                'pageSizeParam' => false
            ],
            'sort' => [
                'defaultOrder' => [
                    'statys' => SORT_ASC
                ]
            ]
        ]);
        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

That works pagination and sort, but filtering stops working. I understand that I'm overriding $dataProvider, but how can I make it work?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Timofeev, 2017-06-22
@MaikMain

I understand that I am overriding $dataProvider

No, you do not redefine it, instead you throw completely different data into a variable.
All settings
'query' => Orders::find(),
            'pagination' => [
                'pageSize' => 10,
                'forcePageParam' => false,
                'pageSizeParam' => false
            ],
            'sort' => [
                'defaultOrder' => [
                    'statys' => SORT_ASC
                ]
            ]

can be done in the searchModel, why are you doing this in an action?
but if you really need it, then this:
public function actionIndex()
    {
        $searchModel = new OrdersSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        $dataProvider->setSort([
                'defaultOrder' => [
                    'statys' => SORT_ASC
                ]
          ]);
         $dataProvider->setPagination([
                'pageSize' => 10,
                'forcePageParam' => false,
                'pageSizeParam' => false
        ]);
        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

Here is the override, and you just took a variable and created a new instance of the class in it.

S
Sergey Tsvetoshenko, 2017-06-22
@tsvetoshenko

all work with the Data Provider takes place in the search method of the OrdersSearch class, there is no need to override it in the controller.
In what form do you display data in the view? GridView or ListView?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question