3

I'm pretty new at Symfony 2 and I was wondering something :

Let's assume I have 2 bundles in my projects. I want to use entities generated from my database in both bundles.

Where am I suppose to generate the entities ? (To me the best way would be outside the bundles but I can't find out how to do that)

Thanks for your help.

Yoot
  • 657
  • 2
  • 14
  • 26

1 Answers1

4

I think there is two solutions, you have to think of the design of your application.

Are you sure you need two bundles ? If the link is so strong between the two, why didn't you choose to make only one bundle ? In this case, you'll just have to generate the entities into this bundle.

Other case : you effectively need two bundles, but in this specific application you need to make a link between the two. In this case, I think you should generate the entities in the bundle where it belongs, and if you need so you can use them in another bundle (thank to use MyApp\MyBundle\Entities\...;). You have to think in terms of generic code when using Symfony, in order to be able to reuse your bundles in other projects. ;)

Jérémy Dutheil
  • 6,099
  • 7
  • 37
  • 52
  • Thanks for your answer, actually in this case I was thinking of one bundle for the main application, and another one for the admin section. But you can also imagine several bundles to manage big modules (ex : blog, shop, user...) : in that case in which bundle would you generate your entities since they are all using the same database ? – Yoot Dec 02 '11 at 10:41
  • I think you're going the wrong way in thinking in term of "big applications" ; it was the idea behind Symfony1.4 but not anymore. Now the idea is to split your application in different modules (blog, shop, user...), and you don't mind if they use the same database (it's quite logical in fact). Each bundle would more be seen as a table than a database. So you put the entities relative to blog into the blog bundle, entities relative to shop into the shop bundle, etc... – Jérémy Dutheil Dec 02 '11 at 10:47
  • Hmmm, I'm ok with that,but it doesn't seem easy to handle when you have to re-generate your entities because of modifications in the database (since you must specify one bundle when generating). Besides, thinking that way doesn't work with the first example I gave you : the client / admin bundles : indeed, you'll need all your entities in both of them, and it's of course not thinkable to duplicate your entities. I wonder if the best solution wouldn't be to generate all the entities inside one specific bundle (ex : EntityBundle). What do you think ? – Yoot Dec 02 '11 at 11:04
  • Au fait, tu es français ?? (Je viens juste de lire ton nom ^^) – Yoot Dec 02 '11 at 11:06
  • Oui je suis français, je vais répondre en anglais tout de même pour les autres qui peuvent nous lire ^^ – Jérémy Dutheil Dec 02 '11 at 11:10
  • Well, to begin with, you can use the command `generate:entities` that generate all your entities in their appropriate bundle when you change your database schema. For the problem client/admin, I wouldn't take the problem this way ; why don't you simply have two controllers in each bundle, one for client and one for admin ? It would be more easy to duplicate to another project, and they can of course heritate from the same layout if you're worrying about display. ;) Client/admin are not logical modules, but just containers for them. – Jérémy Dutheil Dec 02 '11 at 11:10
  • Ok I see what you mean, but is this easy to handle about security ? (Can we easily secure each admin controller of each bundle ?) And how do you generate your entities in their "appropriate bundle" ? The generate:entities command only allowed me to get everything into one bundle (you have to indicate into which bundle your entities will be generated in the command line). – Yoot Dec 02 '11 at 13:08
  • Good question, I don't know this much about Symfony2 but I'm pretty sure you can manage where your entities will be generated. For the security, just prefix all your backoffice route with something, like /backoffice and secure this prefix ;) `/backoffice^` – Jérémy Dutheil Dec 02 '11 at 13:42
  • Ok, thank you, I assume you can access entities from other bundles anyway, so even if they are all in the same one it won't be a problem... I was just wondering about how to have a clean application structure. – Yoot Dec 02 '11 at 14:00