mysql - 将 symfony2 应用程序与 mysql 和 postgresql 一起使用

标签 mysql postgresql symfony doctrine schema

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

对于 postgresql,我需要在多个表中使用 schema="admin"。所以我对实体进行了修改:

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

它在 postgresql 上运行良好。

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

@ORM\Table(name="si_user")

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

谢谢你的帮助。

最佳答案

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

首先,正如@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 所示。 .

关于mysql - 将 symfony2 应用程序与 mysql 和 postgresql 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32734769/

相关文章:

php - CodeIgniter:如何在验证后返回表格行中的所有数据?

database - 使用存储过程模拟 postgresql

sql - 我如何分组连接每一行?

php - Symfony2 XPath 返回重复节点

php - Symfony2 : DateTime object not working

php - 将 MySQL INNER JOIN 的结果合并到一个 PHP 数组中

php - View 页面中显示的链接错误

php - 没有 ON 子句的 Doctrine DBAL 连接

MySQL 将 1 列 VARCHAR 分成 2 列 INT 类型。我有错误 1292 截断不正确的 DOUBLE 值

sql - PostgreSQL 查询不一致