P
P
pink2floyd2020-02-28 21:36:15
Yii
pink2floyd, 2020-02-28 21:36:15

How to call the function of the class from which we take data when calling hasOne/hasMany?

There is a Category class that has a getRequiredQuery method

public function getRequiredQuery() {
        return static::find()->where(['status' => 1])->orderBy(['id' => SORT_DESC]);
    }

Which I call every time so as not to ask each new get field the same where (), etc.
And now I want to take the Category data through hasOne in the Product model, but at the same time I will have to write these conditions again and if I change them in getRequiredQuery I can forget to change them in the bundle. The question is, can I call getRequiredQuery in the hasOne method of the Product model, or can it all be done in another method?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2020-02-28
@pink2floyd

Good evening.
You do not need any of your methods, everything has already been invented for you.
https://www.yiiframework.com/doc/guide/2.0/en/db-a...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question