Yii2 default values for Index data Provider

You are viewing revision #1 of this wiki article.
This is the latest version of this article.

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.