php - 在 Symfony 5 中从多个数据库恢复数据的问题
问题描述
我按照 Symfony 的建议为两个数据库创建了两个连接: https ://symfony.com/doc/current/doctrine/multiple_entity_managers.html
我使用 Symfony 5.0。
我配置了两个实体管理器:
# config/packages/doctrine.yaml
doctrine:
dbal:
default_connection: default
connections:
default:
url: '%env(resolve:DATABASE_URL)%' #bdd-main
erp:
url: '%env(DATABASE_ERP_URL)%' #bdd-erp
orm:
auto_generate_proxy_classes: true
default_entity_manager: default
entity_managers:
default:
connection: default
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
mappings:
Main:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: Main
erp:
connection: erp
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
mappings:
Erp:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/EntityErp'
prefix: 'App\EntityErp'
alias: Erp
对于测试,我在 EntityErp 中创建了一个 Toto 对象(因此对于数据库“bdd-erp”)。
当我尝试保存对象时,我可以做到:
$erpEntityManager = $this->getDoctrine()->getManager('erp');
$toto = new Toto();
$toto->setName('Toto');
$erpEntityManager->persist($toto);
$erpEntityManager->flush();
在数据库“bdd-erp”中,该对象存在。
但是,当我只想恢复所有对象时,它不起作用:
$totos = $this->getDoctrine()
->getRepository(Toto::class, 'erp')
->findAll();
它试图连接到错误的数据库,所以我有这个错误:
An exception occurred while executing 'SELECT t0.id AS id_1, t0.name AS name_2 FROM Toto t0':
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'bdd-erp.toto' doesn't exist
编辑:我试过这个,但仍然是同样的问题:
$emErp = $this->getDoctrine()->getManager('erp');
$totos = $emErp->getRepository(Toto::class)->findAll();
我不明白,我做错了什么?
==> 编辑 - 解决方案: 我找到了解决方案。最后,这是 Symfony 5 的异常。我重命名了实体文件夹“ErpEntity”(而不是“EntityErp”)并且它可以工作(我在 'App\Entity\Erp' 之前尝试过,但仍然是错误)。奇怪,但我可以关闭这个帖子。
解决方案
我找到了解决方案。最后,这是 Symfony 5 的异常。我将实体文件夹重命名为“ErpEntity”(而不是“EntityErp”)并且它可以工作。奇怪,但我可以关闭这个帖子。
推荐阅读
- javascript - 为什么我的 JavaScript 函数在我告诉它在另一个函数运行时停止时继续运行?
- c++ - 巨大的数字而不是 0 和浮点数
- java - 当我尝试运行 appium 自动化项目时,方法 getBinaryPath() 未定义为 Eclipse 中显示的类型 WebDriverManager 错误
- laravel - 原始选择 Laravel - 'ST_Transform' 不是公认的内置函数名称
- java - 在 JDK11 中使用 ToStringBuilder.reflectionToString() 对象到字符串
- android - Android 上的 TensorFlow Lite PoseNet 因内存问题而崩溃
- java - 我应该在事件侦听器中将什么传递给这个 Intent 构造函数?
- docker - 在 Docker Alpine 映像上运行时,带有 DB2 的 ASP.NET Core 在第一次请求时退出并显示代码 139
- python - 数据类在 Python 中忽略 asdict 中的属性
- javascript - 拒绝承诺,但 response.ok 是真的。JavaScript Rails 获取请求“无法在 'Response' 上执行 'json':正文流已读取”