Doctrine orm Symfony 3使用第二个正在使用的数据库
我尝试在symfony3安装中管理2个数据库。 我希望有两个连接,而不是两个管理器,因为第二个数据库只是用于加载外部数据,而不是用于orm 我的身份证Doctrine orm Symfony 3使用第二个正在使用的数据库,doctrine-orm,symfony,Doctrine Orm,Symfony,我尝试在symfony3安装中管理2个数据库。 我希望有两个连接,而不是两个管理器,因为第二个数据库只是用于加载外部数据,而不是用于orm 我的身份证 doctrine: dbal: default_connection: default connections: default: driver: pdo_mysql host: "%database_host%" port: "%da
doctrine:
dbal:
default_connection: default
connections:
default:
driver: pdo_mysql
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
dbsync:
driver: pdo_mysql
host: "%database_sync_host%"
port: "%database_sync_port%"
dbname: "%database_sync_name%"
user: "%database_sync_user%"
password: "%database_sync_password%"
charset: UTF8
orm:
auto_generate_proxy_classes: "%kernel.debug%"
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
在控制器中,我可以使用sql select的第二个数据库:
...->getManager()->getConnection('dbsync');
但是我想在服务中使用它,我不知道在这种情况下如何使用getConnection('dbsync')。my service MajUsers.php
<?php
namespace EntBundle\Service;
use Doctrine\ORM\EntityManager;
use EntBundle\Entity\User\User;
class MajUsers {
private $em;
/**
* @param EntityManager $em
*/
public function __construct(EntityManager $em)
{
$this->em = $em;
}
public function runUpdate()
{
$conn = $this->em->getManager('dbsync')->getConnection('dbsync');
$personnels_sync = $conn->fetchAll("SELECT * FROM xxxxx WHERE etat = 1 AND login !='' ORDER BY xxx, yyyyLIMIT 10");
.....
.....
}
}
我在代码中将em用于某些存储库,它工作正常,但getConnection(“dbsync”)部分不工作。我想我需要在_构造中注入一个容器(条令?),但我不知道是哪一个,我所有的测试都失败了:c/
感谢您的帮助更新:
在
\u构造中
键入针对\doctor\DBAL\Connection
的提示,并定义如下服务:
ent.maj_users:
class: EntBundle\Service\MajUsers
arguments:
- '@doctrine.dbal.dbsync_connection'
感谢@Cerad指出我的错误
旧版: 你可以在文档中找到它 基本上,您应该这样定义服务:
ent.maj_users:
class: EntBundle\Service\MajUsers
arguments:
- '@doctrine.orm.dbsync_entity_manager'
更新:
在
\u构造中
键入针对\doctor\DBAL\Connection
的提示,并定义如下服务:
ent.maj_users:
class: EntBundle\Service\MajUsers
arguments:
- '@doctrine.dbal.dbsync_connection'
感谢@Cerad指出我的错误
旧版: 你可以在文档中找到它 基本上,您应该这样定义服务:
ent.maj_users:
class: EntBundle\Service\MajUsers
arguments:
- '@doctrine.orm.dbsync_entity_manager'
关闭,但他只需要连接,所以类似于“@doctrine.dbal.dbsync_connection”的东西我已经尝试过这个解决方案,Xymanek,但它只针对两个管理器,我需要两个连接!你试过按照@Cerad的建议做吗?我看到你键入了hinted EntityManager,所以我认为你需要该服务,我的朋友我尝试测试Cerad解决方案,但我不知道在服务的u结构中写什么…
\doctor\DBAL\Connection
Close,但他只需要连接,比如'@doctor.DBAL.dbsync\u Connection',我已经尝试过这个解决方案,Xymanek,但它是两个经理的,我需要两个连接!你试过按照@Cerad的建议做吗?我看到您键入了hinted EntityManager,所以我认为您需要该服务,我的朋友我尝试测试Cerad解决方案,但我不知道在服务的结构中写什么…\doctor\DBAL\Connection