具有两个Entitymanager的学说看错数据库

时间:2018-12-04 00:51:03

标签: doctrine

我确实遵循these rules的原则,建立了第二个连接和第二个实体管理器。 “默认”数据库称为revee,而“源”数据库称为reveesrc

什么有效

当我在postUp()方法中使用此代码对迁移进行试运行时:

    /** @var EntityManager $em */
    $em = $this->container->get('doctrine.orm.entity_manager');

    /** @var EntityManager $emSrc */
    $emSrc = $this->container->get('doctrine.orm.source_entity_manager');

    var_dump($emSrc->getConnection()->getDatabase());

    $dates = $emSrc->getRepository('App:Dates')->findAll();

很奇怪,我把数据库reveesrc正确地写成了我们的 !这意味着从连接到实体管理器的映射工作正常。

什么不起作用

但是,下一行会产生错误。

 Base table or view not found: 1146 Table 'revee.dates' doesn't exist"

由于dates是在附加到第二个源实体管理器的Entity文件夹中定义的,所以我认为学说会知道在哪里查找表。将实体映射到另一个源数据库该怎么做?

doctrine.yaml

parameters:
    # Adds a fallback DATABASE_URL if the env var is not set.
    # This allows you to run cache:warmup even if your
    # environment variables are not available yet.
    # You should not need to change this value.
    env(DATABASE_URL): ''

doctrine:

    dbal:
        # configure these for your database server
        default_connection: default
        connections:
            default:
                url: '%env(resolve:DATABASE_URL)%'
                driver: 'pdo_mysql'
                server_version: '5.7'
                charset: utf8mb4
                default_table_options:
                    charset: utf8mb4
                    collate: utf8mb4_unicode_ci

            source:
                url: '%env(resolve:DATABASE_URL_SOURCE)%'
                driver: 'pdo_mysql'
                server_version: '5.7'
                charset: utf8mb4
                default_table_options:
                    charset: utf8mb4
                    collate: utf8mb4_unicode_ci

    orm:
        default_entity_manager: default
        auto_generate_proxy_classes: true
        entity_managers:
            default:
                naming_strategy: doctrine.orm.naming_strategy.underscore
                auto_mapping: true
                connection: default
                mappings:
                    App:
                        is_bundle: false
                        type: annotation
                        dir: '%kernel.project_dir%/src/Entity'
                        prefix: 'App\Entity'
                        alias: App
            source:
                naming_strategy: doctrine.orm.naming_strategy.underscore
                connection: source
                mappings:
                    App:
                        is_bundle: false
                        type: annotation
                        dir: '%kernel.project_dir%/src/EntitySrc'
                        prefix: 'App\EntitySrc'
                        alias: App

src \ Entity \ Dates.php(第一行)

<?php

namespace App\EntitySrc;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\DateSrcRepository")
 */
class Dates
{

    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")

src \ Repository \ DateSrcRepository.php

<?php

namespace App\Repository;

use App\EntitySrc\Dates;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Symfony\Bridge\Doctrine\RegistryInterface;

class DateSrcRepository extends ServiceEntityRepository
{
    public function __construct(RegistryInterface $registry)
    {
        parent::__construct($registry, Dates::class);
    }

1 个答案:

答案 0 :(得分:0)

如果有人遇到相同的问题,我通过在实体中明确指定表并为它添加数据库前缀来解决此问题。如果有人知道那不是解决问题的正确方法,我会很感兴趣。

<?php

namespace App\Entity\Src;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\DateSrcRepository")
 * @ORM\Table(name="reveesrc.dates")
 */
class Dates
{