S
S
Sergey Beloventsev2017-08-25 10:32:44
Yii
Sergey Beloventsev, 2017-08-25 10:32:44

Why has the number of requests increased?

To implement filters in the gridview, I need to get model data several times. I'm trying to organize the gridview itself as follows

[
                'attribute'=>'title',
                'header'=>'Type',
                'filterType'=>GridView::FILTER_SELECT2,
                'filter'=> $models->type,
                'filterWidgetOptions'=>[
                    'pluginOptions'=>['allowClear'=>true],
                ],
                'filterInputOptions'=>['placeholder'=>Html::encode('Select Type')]
            ],
[
                'attribute'=>'provider',
                'filterType'=>GridView::FILTER_SELECT2,

            ],
            [
                'attribute'=>  'quantity',
                'filterType'=>GridView::FILTER_SELECT2,
                'filter'=> $models->quantitys,
                'filterWidgetOptions'=>[
                    'pluginOptions'=>['allowClear'=>true],
                ],
                'filterInputOptions'=>['placeholder'=>Html::encode('Select Quantity')]
            ],

this is how I try to organize in the model
public function __construct($config = [])
    {
        $this->products=$this::find()->asArray()->all();
        parent::__construct($config);
    }
public function getType(){
        return ArrayHelper::map($this->products,'title','title');
    }

    /**
     * @return array
     */

    public function getProviders(){
        return ArrayHelper::map($this->products,'provider','provider');
    }

    /**
     * @return array
     */
    public function getQuantitys(){
        return ArrayHelper::map($this->products,'quantity','quantity');
    }

but the number of requests, if not increased, then remained the same. The question is, can I somehow get all the model data in the database?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2017-08-25
@qonand

The number of requests has increased from the fact that you are making a request in the model constructor. Accordingly, when you make a selection of models (for GridView), you get an array of models and a constructor with a request is called for each of them. If you want to solve the problem - remove the request from the constructor to another place

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question