Question

I have done a few small projects in Symfony 2, they are really simple projects as I am a beginner they have one bundle only and this bundle manages the forms and data access to from 1 to 3 tables. Now someone is interested on one of this bundles and he wants me to develop some other functionality that is going to require some more bundle and they are supposed to use the same database, templates, etc. I have a lot of doubts, but there is one thing that I need to be able to start and is to take the entities and probably some of the forms out of the original bundle let say:

src
|__MyProject
  |___MyBundle
     |__Entity
     |__Form

And I want to have this entities and general forms as and independent folder probably inside MyProject let say:

src
|__MyProject
  |__Entity (General entities)
  |__Form (general forms)
  |___MyBundle
     |__Entity
     |__Form

I do not know if this is the correct approach, but I think I need something like this. I have been reading a few docs about best practices and they do something similar (not exactly the same) but every developer has his own book. The question is how do you tell Symfony that the entities are not any more here and they are some where else, I have tried to change the namespaces paths and the use statements, but it does not work by doing only that, Symfony is still looking for those entities inside MyBundle. Can any one assist me some how or point me out to reliable docs?

Était-ce utile?

La solution

When you want to call entities outside the current name space (vendor/bundle1/entity), you need to reference. 2 Solutions :

namespace vendor\bundle2\Controller;

use vendor\bundle1\Entity\MyEntity;

Or

directly set the full path in the name of your entity

public function indexAction() {

...
$entity = new vendor\bundle1\Entity\MyEntity();
....

}

PS : I don't know if it s related but i remember issues with entity subdirectory with Doctrine

Autres conseils

You wrote in your question that the new bundle will use the same database, templates etc. That means that these bundles are tightly coupled. Therefore you should ask yourself a question:

Will I ever distribute these bundles separately?

If your answer is No. then you should keep those two bundles in one bundle, not in separate bundles. If your answer is Yes., which is probably not this case, then you should have separate bundles.

For separating your bundles, in this case, you should keep it like this:

src
|__MyProject
  |___MyBaseBundle
  |   |__Entity
  |   |__...
  |___MyOtherBundle
      |__Form
      |__...

In your base bundle you should keep all items that are common for all bundles, in your case entities, layout and other resources. In your other bundles you should keep items which are mostly specific for just that bundle.

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