2
2
2462016-01-10 19:15:51
symfony
246, 2016-01-10 19:15:51

How to organize logic in Symfony using inheritance?

Good afternoon
Please tell me the correct organization of the code in the symphony.
The situation, conditionally, is this: the Order entity (order) was originally created, and several actions in the OrderController for creating, searching and editing an order. Accordingly, each action had its own forms (CreateOrderForm, etc.) and views (create_order.html.twig, etc.). An OrderModel was also created, in which all the logic of processing and saving to the database is implemented.
Now, if an order has a certain attribute, it is necessary to implement logic that is partially different from the original one (additional fields appear for information about the order + some differences in processing).
For forms, you can create separate CreateNewOrderForm, etc., which you can inherit from existing ones. Views break up individual templates, transfer them to files, and draw the corresponding ones for different order types. To organize the logic, there is also an option to create a NewOrderModel, which you inherit from the existing one and override the necessary methods.
But then it turns out that the logic of the controller actions is the same, only the created form, the called model and the rendered view differ. Keep several identical controllers - duplication. write something like

if ($orderType == 'new') {
   $form = $this->createForm(new CreateNewOrderForm());
} else {
   $form = $this->createForm(new CreateOrderForm());
}

and similar code blocks for calling the model and rendering views - somehow also smacks of a violation of OOP principles. Moreover, with the appearance of new types, this block of conditions will only grow.
What are the better options?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2016-01-10
Protko @Fesor

What are the better options?

we normally design entities in terms of business logic (NewOrder somehow smells strongly of star wars).
DTOs go between forms and entities. Make the service layer and keep the controllers thin. And you can also use CQRS and EventSourcing, but I feel it's too early, although perhaps it would be even easier.
Actually, there is no specifics in the question. Are you confused by code duplication in controllers? take out duplication in private methods. There should be no logic in the controllers, there is only work with forms and getting data from them (for good).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question