Yii2 default values for Index data Provider

Guys,

maybe a lot of you will not find this interesting but, for me has been really helpful.

I have a list of quotations ( yes, my application manage quotations) in the default index of my controller, and I created different indexes depending on the status of these quotations ( DRAFT, CANCELED, COMPLETED, CREATED ), and what I did next was merge the request array with the default information of the index file.

public function actionIndexdraft() {
        $searchModel = new CotizacionSearch;
        $queryParams = array_merge(array(),Yii::$app->request->getQueryParams());
        $queryParams["CotizacionSearch"]["idEjecutivo"] = 1 ;
        $queryParams["CotizacionSearch"]["status"] = "DRAFT" ;
        $dataProvider = $searchModel->search($queryParams);

        return $this->render('index', [
                    'dataProvider' => $dataProvider,
                    'searchModel' => $searchModel,
        ]);
    }

I the last example, I am creating by default the values for "status" and "idEjecutivo" and it does not matter what you write on the search field, they are going to be always including these information.

I hope that someone finds it useful, I did.

2 0
5 followers
Viewed: 27 636 times
Version: 2.0
Category: Tips
Written by: CTala
Last updated by: CTala
Created on: May 5, 2014
Last updated: 6 years ago
Update Article

Revisions

View all history

Related Articles