What is the best practice of including models/activerecords within a Yii2 module in a way that they are configurable?
These are just some of the problems we face when we want to use an activerecord included inside a module:
Adding events & behaviors to models/activerecords provided by a module. I want to attach events and behaviors to the models included in a module using Yii2's configuration format. How can this be done?
Defining relations with models/activerecords that exist outside of the module. When linking an activerecord contained inside a module to the User activerecord we can rely on
Ỳii::$app->user->identityClass
, but for other custom relations we might need to extend the activerecord. Is there any better approach? Extending activerecord classes from modules somewhat defeats the purpose of modularity.Configuring various other variables within the module/activerecord. Let's say we want to adjust the max string length validation value. In a module Controller, we can always use
$this->module->params
to read any custom value, but we cannot do this from a Model or an ActiveRecord. What are we supposed to do instead?
EDIT: The underlying issue has now been resolved. We can use DI to inject relations into ActiveRecords.
As of July 2017, Yii2 does not allow ActiveRecord dependency injection!
See:
The only way around this is to configure your modules through your
Yii::$app->params
and then use those values inside module ARs (eg. when doing validation).I think you might end up using dependency injection:
Write an extension "\common\extensions\MyBootstrap":
add to your config:
and in your code you have to use
Yii::$container->get()
:which will create
Test2
model instead ofTest1
. If you want this to happen for your ActiveRecord, override this: