R
R
Roman Savitsky2017-09-08 14:00:30
Yii
Roman Savitsky, 2017-09-08 14:00:30

Override find method via function in Model?

Good day everyone!
I connected this extension here: https://github.com/OmgDef/yii2-multilingual-behavior
In modules\catalog\models\Item.php - I connected the behavior
The author of this extension ask to override the find() method in the model, like this

public static function find()
    {
        return new MultilingualQuery(get_called_class());
    }

Or in my case, to select all translations, you need to do this
public static function find()
    {
$q = new MultilingualQuery(get_called_class());
$q->multilingual();
return $q;
}

Then if you do this in the controller:
$model = Item::findOne($id);
All translations of my record are pulled up.
All this is good, of course, but by redefining the find () method in this way in the model, I started getting other errors I didn’t need in other places on the site where I also use the Item class.
The question is how to organize all this through a certain function in the Item class, so that later in the controller, like this, let's get a record from the database?
$model = new Item;
$model = $model->findMultilingual($id);

Thanks in advance !

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
matperez, 2017-09-08
@PRC

You need to create your own class. Some ItemQuery. Inherit it from yii\easyii\components\ActiveQuery and add the MultilingualTrait trait there and return it from Item::find(). And you will be happy.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question