Symfony multi базы данных - PullRequest
       14

Symfony multi базы данных

0 голосов
/ 14 декабря 2018

Мое приложение Symfony управляет видео и комментариями к видео, поэтому у меня есть два пакета videoBundle и commentBundle.

В моем приложении Symfony я использую соединения с несколькими базами данных, каждый клиент подключается к своей базе данных.

Это мой Video.orm.yml

VideoBundle\Entity\Video:
type: entity
table: null
repositoryClass: VideoBundle\Repository\VideoRepository
id:
    id:
        type: integer
        id: true
        generator:
            strategy: AUTO
fields:
    fileTitle:
        type: string
        length: 255
        column: file_title
        nullable: true
    title:
        type: string
        length: 255
  ...
oneToMany:
    comments:
        targetEntity: CommentBundle\Entity\Comment
        mappedBy: video
        cascade: ["persist","remove","merge"]
        joinColumn:
            onDelete: CASCADE

Это мой Comment.orm.yml

CommentBundle\Entity\Comment:
type: entity
table: null
repositoryClass: CommentBundle\Repository\CommentRepository
id:
    id:
        type: integer
        id: true
        generator:
            strategy: AUTO
fields:
    texte:
        type: string
        length: 255
manyToOne:
    video:
      targetEntity: VideoBundle\Entity\Video
      cascade:      ["persist"]
      inversedBy: comments
      joinColumn:
        name: video_id
        referencedColumnName: id
        onDelete: "CASCADE"
        options:
            comment: Video Id

Это мои менеджеры по настройке конфигурации:

doctrine:
dbal:
    default_connection: "%doctrine.default_connection%"
    connections:
        default:
            driver:   pdo_mysql
            host:     "%connections.default.host%"
            port:     "%connections.default.port%"
            dbname:   "%connections.default.dbname%"
            user:     "%connections.default.user%"
            password: "%connections.default.password%"
            charset:  UTF8
            mapping_types:
                enum: string
        custumer1:
            driver:   pdo_mysql
            host:     "%connections.custumer1.host%"
            port:     "%connections.custumer1.port%"
            dbname:   "%connections.custumer1.dbname%"
            user:     "%connections.custumer1.user%"
            password: "%connections.custumer1.password%"
            charset:  UTF8
            mapping_types:
                enum: string
        custumer2:
            driver:   pdo_mysql
            host:     "%connections.custumer2.host%"
            port:     "%connections.custumer2.port%"
            dbname:   "%connections.custumer2.dbname%"
            user:     "%connections.custumer2.user%"
            password: "%connections.custumer2.password%"
            charset:  UTF8
            mapping_types:
                enum: string
orm:
    default_entity_manager: "%doctrine.default_orm%"
    auto_generate_proxy_classes: "%kernel.debug%"

    entity_managers:
        default:
            naming_strategy: doctrine.orm.naming_strategy.underscore
            connection: default
            dql:
                numeric_functions:
                    time_diff: CoreBundle\DQL\TimeDiff
                    rand: CoreBundle\DQL\RandFunction
                string_functions:
                    replace: CoreBundle\DQL\Replace
            mappings:
                VideoBundle: ~
                CommentBundle: ~
                FOSUser:
                    mapping:              true
                    type:                 'xml'
                    dir:                  "%kernel.root_dir%/../vendor/friendsofsymfony/user-bundle/Resources/config/doctrine-mapping/"
                    alias:                'FOSUser'
                    prefix:               FOS\UserBundle\Model
                    is_bundle:            false
        custumer1:
            naming_strategy: doctrine.orm.naming_strategy.underscore
            connection: custumer1
            dql:
                numeric_functions:
                    time_diff: CoreBundle\DQL\TimeDiff
                    rand: CoreBundle\DQL\RandFunction
                string_functions:
                    replace: CoreBundle\DQL\Replace
            mappings:
                VideoBundle: ~
                CommentBundle: ~
                FOSUser:
                    mapping:              true
                    type:                 'xml'
                    dir:                  "%kernel.root_dir%/../vendor/friendsofsymfony/user-bundle/Resources/config/doctrine-mapping/"
                    alias:                'FOSUser'
                    prefix:               FOS\UserBundle\Model
                    is_bundle:            false

        custumer2:
            naming_strategy: doctrine.orm.naming_strategy.underscore
            connection: custumer2
            dql:
                numeric_functions:
                    time_diff: CoreBundle\DQL\TimeDiff
                    rand: CoreBundle\DQL\RandFunction
                string_functions:
                    replace: CoreBundle\DQL\Replace
            mappings:
                VideoBundle: ~
                CommentBundle: ~
                FOSUser:
                    mapping:              true
                    type:                 'xml'
                    dir:                  "%kernel.root_dir%/../vendor/friendsofsymfony/user-bundle/Resources/config/doctrine-mapping/"
                    alias:                'FOSUser'
                    prefix:               FOS\UserBundle\Model
                    is_bundle:            false

Я хочу использовать это отношение для некоторых клиентов и использовать видео без комментариев у некоторых клиентов.

Клиенты, использующие видео без комментариев, не имеют комплекта commentBundle.

Как можноЯ так делаю?

...