0

I'm following the documentation here:

These are the commands

php app/console doctrine:mapping:convert xml ./src/Acme/BlogBundle/Resources/config/doctrine/metadata/orm --from-database --force
php app/console doctrine:mapping:import AcmeBlogBundle annotation
php app/console doctrine:generate:entities AcmeBlogBundle

I see the new Entities here in"

Acme/BlogBundle/AcmeBlogBundle/Entity

But I wanted to know how I could add the Entities into their own namespace like this

Acme/BlogBundle/AcmeBlogBundle/Entity/Foo
Acme/BlogBundle/AcmeBlogBundle/Entity/Bar

This is so I could keep the Entities for Foo and Bar Databases separated.

UPDATE:

Or should it be structured like this:

Acme/BlogBundle/AcmeBlogBundle/Foo/Entity
Acme/BlogBundle/AcmeBlogBundle/Bar/Entity

Thoughts?

Phill Pafford
  • 83,471
  • 91
  • 263
  • 383
  • I'm a bit confused as to what exactly you're asking. You want to create a new namespace for each entity? That makes no sense. Then Foo is /Acme/BlogBundle/AcmeBlogBundle/Entity/Foo/Foo, Bar is /Acme/BlogBundle/AcmeBlogBundle/Entity/Bar/Bar, and they're both the only objects in their own namespace. Is that really what you're looking for? – AmericanUmlaut Nov 29 '12 at 17:00
  • I have multiple databases and I would like to organise the Entities by database, So the Foo Database should go into the Foo directory and the Bar database in the Bar directory. This is just to easily identify what database an Entity is related to – Phill Pafford Nov 29 '12 at 17:16
  • Oh, that's a really interesting problem. I unfortunately have no idea, though :(. My current project also has multiple DBs, but they have the same structure with different data. – AmericanUmlaut Nov 29 '12 at 17:25

1 Answers1

7

If you take a look at How to work with Multiple Entity Managers and Connections section of the documentation, you'll notice that you can bind your bundle entities to one or many entity managers. Each one of them related to a specific database connection.

If for example I've defined two database connections (first_connection and second_connection), I can than add two entity managers as follow,

entity_managers:
    first_manager:
        connection:       first_connection
        mappings:
            MyBundle:
                dir:      Path/To/EntityFolder/Foo/
    second_manager:
        connection:       second_connection
        mappings:
            MyBundle:
                dir:      Path/To/EntityFolder/Bar/

You can then specify the right Entity Manager to use during the two first steps of the entity generation process,

php app/console doctrine:mapping:convert xml ./src/Acme/BlogBundle/Resources/config/doctrine/metadata/orm --from-database --force --em=first_manager --filter=MyTable

Note: The --filter option is used to allow you generate your entities individually.

php app/console doctrine:mapping:import AcmeBlogBundle annotation --em=first_manager --filter=MyTable

php app/console doctrine:generate:entities AcmeBlogBundle

Your entities are then put into the right folders according to the connection the were bound to.

Ahmed Siouani
  • 13,701
  • 12
  • 61
  • 72
  • Must have missed this, Thanks – Phill Pafford Nov 29 '12 at 20:44
  • [InvalidArgumentException] Bundle "dir" does not exist or it is not enabled. – Phill Pafford Nov 30 '12 at 20:18
  • This works except I have one small issue: --filter=MyTable doesn't work. If I remove the option I get all the tables in the database, if I add the option I get: "No Metadata Classes to process.", Thoughts? – Phill Pafford Dec 05 '12 at 18:05
  • @Phill, you are correct. I either getting "db does not have mapping information" or I am getting "No Metadata Classes to process." error. See [**this comment for explanation**](https://stackoverflow.com/questions/10371600/generating-a-single-entity-from-existing-database-using-symfony2-and-doctrine/10614792#comment40007517_10614792) – webblover Sep 01 '14 at 17:39