使用Symfony2应用程序与MySQL和PostgreSQL

4
我正在使用Symfony2框架开发一款应用程序。该应用程序需要在不同的服务器上运行,其中一个服务器使用MySQL,另一个使用PostgreSQL。
对于PostgreSQL,我需要在多个表中使用schema="admin"。因此,我在实体中进行了如下设置:

@ORM\Table(schema="admin",name="si_user")

在PostgreSQL上这很好用。
但是,当我尝试更新或创建模式SQL时,Doctrine无法创建或找到该表。如果我删除schema="admin",它就可以正常工作,如下所示:

@ORM\Table(name="si_user")

你有什么解决方案来保留schema属性并且MySQL不使用schema属性吗?
谢谢你的帮助。

当我尝试更新或创建模式 SQL 时,Doctrine 找不到或创建表。你的意思是当使用 schema="admin" 定义时,Doctrine 无法在 MySQL 上创建/找到表格吗? - eggyal
是的,这正是我想表达的意思。 - gaet
4
使用两个连接和两个实体管理器,可以避免注释之间的冲突。详见:http://symfony.com/doc/current/cookbook/doctrine/multiple_entity_managers.html - Jekis
1个回答

0

解决方案1:同一实体类,多个映射

首先,正如@Jekis所指出的那样,您需要不同的连接和实体管理器。

# Doctrine Configuration
doctrine:
    dbal:
        default_connection: default
        connections:
            default:
                driver:   pdo_mysql
                host:     "%database_host%"
                port:     "%database_port%"
                dbname:   "%database_name%"
                user:     "%database_user%"
                password: "%database_password%"
                charset:  UTF8
            postgres:
                driver:   pdo_pgsql
                host:     "%pg_host%"
                port:     "%pg_port%"
                dbname:   "%pg_name%"
                user:     "%pg_user%"
                password: "%pg_password%"
                charset:  UTF8
    orm:
        default_entity_manager: default
        auto_generate_proxy_classes: "%kernel.debug%"
        entity_managers:
            default:
                connection: default
                mappings:
                    AppBundle:
                        type: yml
                        dir: Resources/config/doctrine/default
            postgres:
                connection: postgres
                mappings:
                    AppBundle:
                        type: yml
                        dir: Resources/config/doctrine/postgres

其次,由于您在相同实体上的每个连接都需要不同的映射信息,我猜您可能需要除了注解之外的其他元数据。 例如,可以使用yml文件。 这样,您可以为每个实体管理器定义特定的映射。
MySQL:
# Resources/config/doctrine/default/User.orm.yml
AppBundle\Entity\User:
    type: entity
    table: user
    repositoryClass: AppBundle\Repository\UserRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        name:
            type: string
            length: 255
    lifecycleCallbacks: {  }

Postgres:

# Resources/config/doctrine/postgres/User.orm.yml
AppBundle\Entity\User:
    type: entity
    schema: admin
    table: user
    repositoryClass: AppBundle\Repository\UserRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        name:
            type: string
            length: 255
    lifecycleCallbacks: {  }

然后,在调用doctrine:schema:update命令时,您还可以传递要更新的数据库的实体管理器。

php bin/console doctrine:schema:create
php bin/console doctrine:schema:create -em postgres

这是我做的测试: https://github.com/vtoulouse/multiple-mapping-test 解决方案2: 多实体类
或者,如果你想要使用注释,你必须要复制你的实体类,就像这个答案所展示的那样。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接