I'm learning Symfony and I'm trying to figure out where to put custom actions over an Entity...
For example, if I have an entity Order
, where to put $order->complete()
? Or $order->sendToProduction()
, $order->queueForDelivery()
?
Those are just examples, I have complex entities and I must perform on them many actions.
In the Controller?
In the Entity?
In the EntityController?
What else? Do I have to create services? Utility classes?
If the work can be done inside a signle entity (and it's relations of course) then it should be placed there. I mean, if the operation is about changing entity's internal state.
Otherwise, if this job need to use other parts of application like database, or is performed on multiple not related entites, then I would suggest using services.
That's what where are for. Service is basically a class that can do anything. Using Service container, you can pass any dependencies to it so it's very flexible and easy to use.
For example $order->queueForDelivery()
. That may mean a few different things:
status
to queued_for_delivery
- then it should be in Order
entity class$order
should be put in the Queue
that is other entity class, then it should be in Queue
class like $queue->addOrder($order)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With