Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integrating Bundles (Related Doctrine 2 Entities) in Symfony 2 and Coupling

As I understand, Bundles is to separate functionality. Suppose I have a UserBundle & a BlogBundle. Then my BlogBundle:Post will have an author field that references UserBundle:User. Doesn't that defeats the purpose of Bundles? I cant swap another UserBundle in? How can I do it a proper way? Or is this the best it can go?

like image 733
Jiew Meng Avatar asked Feb 01 '26 23:02

Jiew Meng


2 Answers

If you are uncomfortable with those dependencies between bundles (I personally agree with you), you can create your model outside of any bundle. This can be configured like so…

doctrine_mongodb:
    document_managers:
        default:
            mappings:
                model:
                    type: annotation
                    dir: %kernel.root_dir%/../src/MyApp/Model
                    prefix: MyApp\Model\
                    alias: Model

Configuration for the ORM would look similar.

like image 132
Kris Wallsmith Avatar answered Feb 04 '26 14:02

Kris Wallsmith


Bundles are meant to separate functionality as much as possible, but that doesn't mean that some won't have dependencies on others.

I think it's interesting that even some of the Symfony2 components, which are supposed to be stand-alone libraries, have dependencies. For example, the HttpKernel class depends on classes from the HttpFoundation and EventDispatcher components.

In your example, if you needed to swap in another UserBundle, you'd have to take the necessary steps to ensure your BlogBundle's dependency is still fulfilled. This may mean you need to refactor.

So, the purpose of bundles isn't defeated because one references the other. Bundles are still advantageous to use even if they're not always 100% uncoupled.

like image 29
Steven Mercatante Avatar answered Feb 04 '26 15:02

Steven Mercatante



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!