Question

In my Symfony2 project, I have a ModelBundle which holds my entities, and other bundles for the frontend, backend, etc, which utilise that ModelBundle.

I have a couple of model-specific functions that perform some entity-specific tasks. Where do I put these?

  1. Do I create a seperate 'model' folder next to my Entity folder, and put all those functions in there?
  2. Do I create a controller in my 'ModelBundle' that holds these functions as actions? This might be weird, since controllers, in my mind, take HTTP requests and return an HTTP response, so it's not applicable in this context.
  3. Do I put them inside the entity classes themselves? Is this nice? And is there a chance that they are over-written when I re-generate my ORM?
  4. Am I looking for custom repositories? I can make those and put them into a 'model' folder or something. http://www.doctrine-project.org/docs/orm/2.0/en/reference/working-with-objects.html#custom-repositories

P.S.: I love Symfony2, but I will be very happy when everyone has a defined set of best practices for the framework :-)

Était-ce utile?

La solution

It'd be helpful to know what type of model-specific functions you plan on writing.

If the functionality is for an entity you've already fetched from the database, it belongs in the entity class.

If the functionality has to do with finding an entity or group of entities based on specific criteria, it belongs in a custom repository class.

I'm not a fan of thinking that model classes and entity classes should be different. IMHO, it's an unnecessary level of abstraction.

You're correct in thinking that this functionality doesn't belong in the controller.

If you do go with custom repositories, I suggest keeping them either in an "Entity" folder, or a "Repository" folder. I personally keep mine in "Entity", but that's likely a side effect of having used Doctrine 1 for so long and being used to 'Table' classes.

I hope this helps, but if you have any questions please post some more details of what you hope to accomplish.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top