微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我用symfony2框架开发了一个应用程序.
应用程序需要在不同的服务器上工作,一个使用mysql,一个使用postgresql.

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

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

它在postgresql上运行正常.

当我尝试更新或创建模式sql时,doctrine不会创建或查找表.当我删除schema =“admin”时,它的工作就找到了.

@ORM\Table(name=”si_user”)

你有任何解决方案来保持架构attribut和MysqL不使用架构attribut?

谢谢你的帮助.

解决方法:

解决方案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:多个实体类

或者,如果要使用注释,则必须复制实体类,如this answer所示.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐