V
V
Vadim2017-02-16 13:14:54
Yii
Vadim, 2017-02-16 13:14:54

Yii2 how to extend a model when a module is included?

Good afternoon and straight to the point.
Given:
Yii2 + MongoDB
There is a module that should be connected dynamically:
1) Module Departments (id + name)
If the module is connected, we give the user the ability to add and edit departments.
2) If the departments module is active, then you need to be able to put the DepartmentID property in the User collection.
Actually a question how it is correct to expand (add) functionality of departments for users.
Based on:
Departments ( id + name )
Users ( id + name + departmentId )
If you turn off Departments
Users (id + name)
The user will be able to change/see only the id+name fields.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Doniy, 2017-02-16
@doniys_a

The strategy is possible.
By adding - everything is clear. When the module is connected, there is access to its controllers, when not, there is no access.
About users and connections.
The simplest option is
if (isset(Yii::app()->modules['moduleName'])) {...}
But there will be a lot of places like this, the second way you can create a base strategy class that will create one object at the input with all the necessary logic, or a factory of objects - this way it will be easier to scatter the logic, use the strategy / factory in all actions, inside which the required class for functionality will already be slipped, if the module is available, one, if not - the other.
In general, you need to organize the logic so that the code is not duplicated and both cases do not know about each other.
It is also quite suitable (even better than the above) DI-container for solving this problem and splitting the global task into many small ones and implementing small ones, then linking them into a tool.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question