In symfony2 the doctrine generator creates a class for each entity. So the approach to add new behavior to these entities (please correct me if I'm wrong) is to add the new custom methods directly in those generated classes.
I'd rather like the approach of symfony 1 where an empty subclass was created and custom methods were placed there. But I'm struggling to use this approach in Symfony2.
Is there a way to generate (and then be able to update) this "Base Entity & SubClass" structure?
If not, why is it better to have all generated and custom code all together?
Edit
I've been playing a bit with the entity generator, both generate:doctrine:entity and doctrine:generate:entities (which are different commands), and analyzing the your answers. It seems that another disadvantage of having all in the same file is the update/regeneration of the entities (for example when a new field is added, and the getters and setters need to be regenerated). With these commands you can set the 'regenerateEntityIfExists' but that overrides the old file, deleting you custom code.
I'm thinking to extend the Doctrine\Bundle\DoctrineBundle\Command\GenerateEntitiesDoctrineCommand\GenerateEntitiesDoctrineCommand to build the old structure (base class with generated code and empty subclass for custom code) which allowed having the custom code separated from the generated one, be able to update the entities to add/remove fields, etc.
But I still wonder why did the development team decided to change the approach used in Symfony1 to this new one. Which are the advantages?
Thanks!
If the logic only involves a single Entity, put it into the Entity class directly. If the logic involves multiple entities, create a new class in a separate namespace and make it a service. Inject the entities you want to perform logic on into this service object and perform the logic there.
This helps you keep a separation of concerns in your entity classes and promotes reuse and testability of your service objects.
For example, if you have a Product entity for products and a Tax entity for tax rates for those products, you might make a ProductTaxManager object and inject the Product and Tax entities into this object to do the calculations.
I'm not sure if the service container was much a part of the original Symfony. but you should definitely do some reading at http://symfony.com/doc/2.2/book/service_container.html
By the way, a repository class should only be used for database queries that involve the Entity by itself only, example being FindAllSortedByAddedDate and such.