2
votes

Because we don't have a lot of choices, with our team we plan to build two different applications with Symfony 2 (an old Back-Office and a new API), stored in two different GitHub repositories. But with one and only one database, and shared doctrine entities.

Potentially one entity in each project (therefore two), "pointing" on the same table in the database.

Is it definitely a bad idea? Is there a solution to share entities?

1
What does "will have the same name, and a unique table in the database" mean? Identical entity but different table?Cerad
No, one entity in each project (therefore two), "pointing" on the same table (just edited)Benoit Mariaux
You can move your entities into their own bundle (and probably their own repo) and then just share the bundle between the two apps. Fairly common as long as the hassle of maintaining multiple repositories is worth it.Cerad

1 Answers

5
votes

Theoretically you could do this with Symfony bundles.

You can create a shared 'core' bundle with all of your entities and other shared code, and put it into a separate repository. You will than be able to integrate this bundle into the 'AppBundle' of each of your applications.

But since your applications will be sharing the same database, you'll need to make sure both applications use exactly the same version of your core bundle at all times. This can make it a little harder to maintain.

A simpler approach might be to create one application with three bundles. One for business logic and two as separate presentation layers.

Symfony 4 (edit)

As I stopped using Symfony since version 3 came out, I'm a bit hesitant on answering this in context of Symfony 4's 'bundleless' approach but I'll give it a try as it seems a pretty popular question.

So even though you're not required to create bundles anymore, you still can create them. And it is in face what the docs recommend if you're sharing code between applications. I feel like this applies equally to monorepos.

Alternatively, you could abstract the whole persistance layer into a shared library. This would allow you to share the same set of entities between completely different frameworks.