A
A
Akhat Moldabekov2016-08-31 15:22:52
Yii
Akhat Moldabekov, 2016-08-31 15:22:52

How to add search terms to ActiveRecord in Yii2?

I have an order index page. There is a search and sorting. The controller uses the searchModel and writes data from it to the dataProvider. I need to add one more condition for selecting elements so that it is not displayed in GET. Type

$data = Orders::find()->where(['subdivision_id' => Yii::$app->user->getUserIdentity()->getSubdivisionId()])->all();

and I need to filter the data $dataagain with the searchModel parameters that are stored in the GET array
And then only display them here:
<?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

            'id',
            'sender_address',
            'reciever_address',
            'currency_id',
            ['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>

so that in the end it would $dataProviderstore in itself $data+ $searchModeland in GET only data on sorting and searchModel filter would be displayed

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Akhat Moldabekov, 2016-08-31
@ahatnya

and I fucking understand. in OrdersSearch it was necessary to dig deeper)))))
here:

public function search($params)
    {
        $query = Orders::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        $manageer_id = 5;

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'currency_id' => $this->currency_id,
            'said_weight' => $this->said_weight,
            'fact_weight' => $this->fact_weight,
            'pay_type' => $this->pay_type,
            'dostavka_price' => $this->dostavka_price,
            'pre_pay' => $this->pre_pay,
            'post_pay' => $this->post_pay,
            'post_pay_days' => $this->post_pay_days,
            'international' => $this->international,
        ]);

        $query->andFilterWhere(['like', 'sender_address', $this->sender_address])
            ->andFilterWhere(['like', 'reciever_address', $this->reciever_address])
            ->andFilterWhere(['like', 'sender_phone', $this->sender_phone]);

        return $dataProvider;
    }
}

on this line:
you can immediately write the selection condition
$query = Orders::find()->where(['id' => 555, 'active' => 1]);

A
Andrew, 2016-08-31
@mhthnz

In serachModel, before outputting the search function, insert the condition into $query

public function search($params)
    {
        ............
        $query->andWhere(['subdivision_id' => \Yii::$app->user->getUserIdentity()->getSubdivisionId()]);
        return $dataProvider;
    }

But it is better to pass subdivisionId, or transfer the user model from the controller to the model and use it already in the model, and not request data from the model.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question