Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 - Doctrine Entity Subdirectories

Basically, i have a branch of code that connects to three different entity managers (and subsequently, three different MySQL databases). Unfortunately, two of the databases have tables that are named he same thing, so i cant have all the entities in the same directory, and for structure, id like to avoid that anyways.

So, my question is... How would i set it up so i can have a single entity bundle, with sub-directories under Entity\ and Resources\config\doctrine\

e.g.

Acme\EntityBundle\Entity\DB1\Test.php
Acme\EntityBundle\Entity\DB1\Acme.php
Acme\EntityBundle\Entity\DB2\Test.php
Acme\EntityBundle\Entity\DB2\Foo.php
Acme\EntityBundle\Entity\DB3\Bar.php

and the same idea for Resources\config\doctrine\

The configs are very vague on what to do here...

like image 938
Ascherer Avatar asked Jan 25 '13 21:01

Ascherer


2 Answers

You only need to rename your doctrine configurations files.

So for example:

  • Bundle/Entity/DB1/User.php
  • Bundle/Entity/DB2/User.php

results in the respective config files (.yml in this case, change as you please)

  • Bundle/Resources/config/doctrine/DB1.User.orm.yml
  • Bundle/Resources/config/doctrine/DB2.User.orm.yml
like image 57
Marcel Burkhard Avatar answered Nov 08 '22 08:11

Marcel Burkhard


If you just need the bundle to store entities, I'd rather suggest to store them outside. This way you'd decouple them from the framework.

In both cases configuration is similar. Reference is accessible in the official docs.

Here's an example:

#app/config/config.yml
doctrine:
    orm:
        auto_generate_proxy_classes: %kernel.debug%
        auto_mapping: true
        mappings:
            AcmeDB1:
                type:       xml
                is_bundle:  false
                dir:        %kernel.root_dir%/../src/Acme/Entity/DB1/config
                prefix:     Acme\Entity\DB1
                alias:      AcmeDB1
            AcmeDB2:
                type:       xml
                is_bundle:  false
                dir:        %kernel.root_dir%/../src/Acme/Entity/DB2/config
                prefix:     Acme\Entity\DB2
                alias:      AcmeDB2

Few notes:

  • dir refers to the mapping directory, if you use annotations this should be directory where your entities are placed
  • prefix is part of your entity's namespace and it should be unique
  • alias makes that you can refer to your entities with shorter namespaces, like AcmeDB1:User
like image 39
Jakub Zalas Avatar answered Nov 08 '22 10:11

Jakub Zalas