Symfony 2.5 repositories and special classes for handling form input and updating data

From what I’ve gleaned from Symfony 2.5 documentation, repositories are best when they’re associated with a distinct entity and have a narrow use for that entity and its other related entities, these are logical associations even though all repositories are still reusable, even from other bundles.  Services are more global and easier to instantiate, not to mention that any dependencies of service objects can also be defined in the services.yml file so the Entity Manager or some other dependent classes can also be automatically injected as needed.

Also, the Symfony documentation suggests that any actions/queries contained in repositories should only retrieve information (act as a “getter”) and not alter any data, as opposed to classes called from a service where their utility is interchangeable as database level “getters” and “setters”.  That said, if you need objects that are easily called from within the controller where you may need to alter data, the Symfony documentation is a little thin on the “best practices”.  I think it’s so much easier to call on these actions/queries in the same repositories you’ve already instanced.

For more complex form projects in the past, I have preferred putting such “form handler” objects that need to actually alter data inside a “/Handlers” folder within the bundle’s “/Forms” folder in order to mirror the organization of “/Repositories” while differentiating the purposes of each, just to follow the “best practices” recommended by our friends at Sensio Labs.

For example, I create a new class that might be called upon to alter certain child entities, perhaps an entity that is a collection of entities for some complex form, like so:  “/MyBundle/Forms/Handlers/MainFormHandler.php”.  Then, within this “MainFormHandler.php” class, the Entity Manager is instanced and I can then call on different methods for the purpose of inserting and updating database data.  If you are PHP developer with Symfony experience, I would love to hear your take on repositories, services, and the special “Handler” classes I mention above.

– Aaron Belchamber

Senior Web Developer
Celebrating 20 years of evolutionary marketing

About Author:

Senior Cloud Software Engineer and 25+ years experienced video production, video editing and 3D animation services for a variety of global clients including local video production here in Jacksonville, Florida.

Leave a Comment

Your email address will not be published. Required fields are marked *