MarBer
MarBer

Reputation: 595

Yii2 access to module instance from model

I want to know if there is a way to access at the module instance from a model. In my module configuration I have some components which I need to call in a model class, the model is defined only inside my module. I know the method Yii::$app->getModule('my-module') but this mean that the module have to be defined every time with the same name, from controller's module I can access as $this->module->component-name, something similar in the model?

Upvotes: 1

Views: 1164

Answers (2)

lalithkumar
lalithkumar

Reputation: 3540

Yes you can access the module instance like this:

$module = MyModuleClass::getInstance();

Then you can get id via

$module->id

For more information check here

Upvotes: 1

Bizley
Bizley

Reputation: 18021

You can use ModelClassName::getInstance() but right now it works only when module has been already initialized. This should be changed around 2.0.15.

Upvotes: 4

Related Questions