Question regarding best practices for moving functionality away from the controllers

Hello, i would like to ask how it would be best to move functionality (even CRUD) away from controllers and if such a task would be an overkill on certain cases.
For example i have a CRUD CustomerController which instantiates a CustomerManager object and calls e.g. an update method (passing the POST data as method parameters) which does the actual DB update for that particular customer.
Is there a better way to achieve a similar approach?


That’s alright. The name for this manager usually is either Repository or Manager.

If you have more logic it becomes more interesting. Some Yii 3 docs may be helpful:

Thank you for your quick reply. I am currently migrating a PMS (written in CI 2) to Yii2. To be honest i would have loved if Yii3 was ready since a more DDD approach on the subject would be better & as far as i can tell Yii3 is taking such an approach.

Another issue i am facing is for example AR models (Entities) that have related models such as Customer & CustomerPhone (or even worse a Reservation model that has several related models). If i wish for example to add more CustomerPhones to the Customer without persisting the model (e.g. testing purposes or some dynamic way of object creation) i would have to create a new property in the Customer object (e.g. PhonesArray = []) and add/remove the Phones from within that “collection” since i cannot access the relation property (e.g. @property Phone[] $phones).

And then i would need to access different methods for retrieving the data from the DB and different methods for retrieving the data from the object.

Is there a better way to approach this? Should i create similar POPO objects for Customer and keep the AR model of Customer only to be used by a Repository class which will retrieve the data from the DB, create POPO Customer object & return that instead? (On second thought by doing so i would lose any advantage of Yii regarding DataProviders, GridView etc.)

Any opinions regarding my thoughts are most welcome.

Yes. We need a bit more time for it in order to release a high quality platform but overall it already works.

In case you’re using repositories already and not using ActiveRecord much, it may make sense to stick to DBAL within repositories ignoring ActiveRecord.

There’s SqlDataProvider, ArrayDataProvider and you can implement your own if needed.

Overall by not using AR you’re losing some convenience but is more flexible about how to retrieve and store your data.