jeudi 22 juin 2017

Symfony3 - where should calculation on an Entity live?

I have a Symfony 3 project with an Entity that has a One-To-Many Self-Referencing relationship. Ie an object of type Post can have one parent, and many children.

Other attributes include a financial value which will need to be totalled at some point. If called on the parent, it should total each child's value. Should this calculation exist as a method on the Doctrine Entity, or be performed elsewhere such as within a Controller or Service?

EDIT for clarity:

I basically need to write a function getTotal() that will operate on an an instance of Post and, since I can't find best practice anywhere, I was wondering whether this should be written as a method in the Post class (e.g. $post->getTotal()) or instead live in a controller or service, and take an argument (e.g. $result = $fooService -> getTotal($post);)

Aucun commentaire:

Enregistrer un commentaire