P
P
Pavel Bykov2020-05-28 16:48:21
Yii
Pavel Bykov, 2020-05-28 16:48:21

Does the Yii2 Behavior have a changing SELECT query in the database?

Hello, I want to automatically drive in certain WHERE conditions in each selection from the model, I think that it is completely possible to do this through behaviors, but I can’t help but find events that would catch SELECT queries in the model, and be able to customize this query.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Timur Khudiyev, 2020-05-28
@mafof

I could be wrong, but it looks like this

public function actions()
    {
        $actions = parent::actions();
        $actions['index']['prepareDataProvider'] = [$this, 'prepareDataProvider'];

        return $actions;
    }

    public function prepareDataProvider()
    {
        return new ActiveDataProvider([
            'query' => MyModel::find()->andWhere(['some_field' => $fieldValue])
        ]);
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question