Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to share a Symfony2 model with several projects

We are creating a SaaS that monitors certain assets. This means it takes in data, saves it, and displays it in a webinterface.

For this, we have a few components that we created with/are moving to Symfony2:

  • a frontend web application, where users can view their data
  • a backend administrative web application, where we create new monitors, users, etc.
  • an API
  • an application that retrieves the received data from a queue and puts it in our database (this is now a seperate script, but I'm thinking of reworking this as a Symfony command that is called by cron)

All these four applications share the same model: our main database that holds all the users, monitors, and data.

My question is: how should I structure these projects in Symfony2?

  1. Do I create a seperate bundle which holds the entities for my database, and have the four projects include those entities and work with them?
  2. Can I make a 'model' directory in my Symfony app folder, which is used by all the bundles in my /src directory?
  3. Some other, cleaner way to do this?

Option 1 seems a bit weird, since a bundle, to my understanding, needs routing, views, controllers, etc. Using it for just entities would be a bit weird.

Option 2 seems alright, since the /app folder is considered 'communal' anyway for everything that is in the /src folder (since, for example, parameters reside there as well). However, there is no 'model' folder there, and I'm not sure that there should be?

I understand that there are very few 'best practices' out already for Symfony 2, since it's brand new. But I wanted to see if there are any practices more preferable then others, in your opinion.

Any feedback is more then welcome. Thanks in advance,

Dieter

like image 369
Dieter Avatar asked Sep 21 '11 16:09

Dieter


2 Answers

What I am currently doing is the first option: create a separate bundle for your entities. That's where I store fixtures, entities, forms and entity-related tests.

A bundle does NOT need to have routing, controllers, views etc. I've actually seen a blueprint bundle, and all it does is ship blueprint-css resources with it so they can be easily reused in projects.

As for adding models to the app directory... I wouldn't like that. I see the app directory as a place where all the configuration should be. Even though you can override views under app/Resources, whenever I want to override something I create a new bundle.

like image 195
Nemanja Miljković Avatar answered Nov 14 '22 20:11

Nemanja Miljković


I haven't used this technique myself in a real world symfony2 app - but it's a pointer as you asked for any feedback.

The service container seems to be the Smyfony2 method to make services available globally. So in your case the model access objects will be defined as services as discussed in the provided link, and then can be used from any bundle.

Now in which bundle do the service objects go? We can put them into a separate bundle as they are shared among other bundles. However, I assume the model would not be perfectly symmetrical for all the bundles, so we can put the shared model into a separate bundle, and put bundle specific entities into the bundle itself. Then injection techniques discussed in the link above can be used to provide a full model specific to each bundle.

This seems to provide maximum de-coupling.

I'm interested in any feedback on this too as it's a common design scenario.

Regards.

like image 1
Basel Shishani Avatar answered Nov 14 '22 18:11

Basel Shishani