我确实遵循了这些规则,在原则上建立了第二个连接和第二个实体管理器。 “默认”数据库称为
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
是在附加到第二个源实体管理器的实体文件夹中定义的,我认为学说会知道在哪里查找该表。我需要做什么才能将实体映射到其他源数据库?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);
}
<?php
namespace App\Entity\Src;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\DateSrcRepository")
* @ORM\Table(name="reveesrc.dates")
*/
class Dates
{