【问题标题】:Use symfony2 application with mysql & postgresql将 symfony2 应用程序与 mysql 和 postgresql 一起使用
【发布时间】:2015-12-20 11:42:10
【问题描述】:

我用 symfony2 框架开发了一个应用程序。 该应用程序需要在不同的服务器上运行,一台使用 mysql,一台使用 postgresql。

对于 postgresql,我需要在几个表中使用 schema="admin"。所以我对实体做了:

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

在 postgresql 上运行良好。

当我尝试更新或创建模式 sql 时,学说不会创建或查找表。当我删除 schema="admin" 时找到它的工作。

@ORM\Table(name="si_user")

你有什么解决方案来保持模式属性和mysql不使用模式属性?

感谢您的帮助。

【问题讨论】:

  • "当我尝试更新或创建schema sql时,doctrine don't create or find the table."——你的意思是说Doctrine无法创建/找到MySQL 上的表,当使用 schema="admin"? 定义时?
  • 是的,这正是我的意思。
  • 使用两个连接和两个实体管理器,注解不会冲突symfony.com/doc/current/cookbook/doctrine/…

标签: mysql postgresql symfony doctrine schema


【解决方案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

方案二:多个实体类

或者,如果你想使用注解,你将不得不复制你的实体类,如this answer所示。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-12
    • 2018-07-05
    • 2011-11-02
    相关资源
    最近更新 更多