这就是我正在尝试做的事情:
namespace BundleTwo\Controller;
use BundleOne\Entity\TestEntity;
class TestController
{
public function pageAction()
{
$insert = new TestEntity();
$insert->fieldName('test');
$em = $this->getDoctrine()->getManager('dbcon2');
$em->persist($insert);
$em->flush();
}
}
…所以上面的代码位于 Bundle 2 中,它使用 Bundle 1 中的实体 - 但它需要使用 DB connection2。
我可以用这个http://symfony.com/doc/current/cookbook/doctrine/multiple_entity_managers.html设置两个数据库连接,我可以看到如何定义 bundle 使用哪个 EM,但我想要一个 bundle 有 2 个连接。
我认为上面的代码可以工作,但是当我运行 app/consoledoctrine:schema:update --em=dbcon2 时,它不会填充数据库中的任何内容,也不会检测到任何实体。我想我需要以某种方式让它认识到 BundleOne\Entity\TestEntity 也被 Bundle 2 和 DB 2 使用。
如果我可以将设置放入 BundleTwo 中来设置用于 BundleOne\Entity\TestEntity 的数据库连接,那么就可以做到这一点。
最佳答案
您可以使用mapping
属性指定哪些包包含实体元数据
doctrine:
orm:
default_entity_manager: dbcon1
entity_managers:
dbcon1:
connection: dbcon1
mappings:
BundleOne: ~
dbcon2:
connection: dbcon2
mappings:
BundleOne: ~
关于php - Symfony2 - 一个 bundle 中的两个数据库连接 - 似乎无法设置它 - 请参阅我的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20439437/