Generate Entities with Doctrine into separate name

2019-01-29 12:07发布

问题:

I'm following the documentation here:

  • http://symfony.com/doc/current/cookbook/doctrine/reverse_engineering.html

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?

回答1:

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.