沃梦达 / 编程问答 / php问题 / 正文

使用 Doctrine 生成实体到单独的命名空间中

Generate Entities with Doctrine into separate namespace(使用 Doctrine 生成实体到单独的命名空间中)

本文介绍了使用 Doctrine 生成实体到单独的命名空间中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注这里的文档:

I'm following the documentation here:

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

这些是命令

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

这样我就可以将 Foo 和 Bar 数据库的实体分开.

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

更新:

或者应该是这样的结构:

Or should it be structured like this:

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

想法?

推荐答案

如果你看看 How要使用文档的多个实体管理器和连接部分,您会注意到您可以将捆绑实体绑定到一个或多个 实体管理器.它们中的每一个都与特定的数据库连接相关.

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.

例如,如果我定义了两个数据库连接(first_connection 和 second_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/

然后,您可以指定在实体生成过程,

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

注意: --filter 选项用于允许您单独生成实体.

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.

这篇关于使用 Doctrine 生成实体到单独的命名空间中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用 Doctrine 生成实体到单独的命名空间中

基础教程推荐