1

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?

lalithkumar
  • 3,480
  • 4
  • 24
  • 40
MarBer
  • 535
  • 1
  • 5
  • 22

2 Answers2

4

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.

Bizley
  • 17,392
  • 5
  • 49
  • 59
1

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

lalithkumar
  • 3,480
  • 4
  • 24
  • 40
  • Did you check this? @MarBer – lalithkumar Jul 24 '17 at 12:29
  • Yes thanks for the rapid answer and for the edit (today is monday!) – MarBer Jul 24 '17 at 12:50
  • glad to help you @MarBer – lalithkumar Jul 24 '17 at 12:51
  • @MarBer Bizley provided right answer, lalith provided wrong answer and just copied right one from Bizley and edit his, that's just not nice... – Yupik Jul 24 '17 at 12:53
  • @lalithkumar - i see edit history and time of adding answer. Please don't lie. – Yupik Jul 24 '17 at 12:57
  • I choose lalithkumar answer because he edit my question before answer, the answer, if we ignore the edit, are the same. How we can resolve this? Sorry @Bizley for not choose your answer but I have consider, like I write above, the edit work done by lalithkumar, if I can do something... – MarBer Jul 24 '17 at 13:04