Php 在Symfony中,是否可以为同一个捆绑包使用两个具有不同数据库的不同文档管理器?
我的目标是让两个不同的文档管理器连接到共享相同数据库模型的不同数据库 我已经对数据库模型进行了重大更改,我想编写一个自定义迁移脚本,从旧模型检索对象,读取其值,然后在新模式上使用旧对象的信息创建一个新对象 我在这里发现了一个相关的stackoverflow问题: 但是,此解决方案建议对每个数据库使用不同的前缀,并将数据模型的类存储在不同的文件夹中:Php 在Symfony中,是否可以为同一个捆绑包使用两个具有不同数据库的不同文档管理器?,php,symfony,doctrine-orm,doctrine-odm,doctrine-mongodb,Php,Symfony,Doctrine Orm,Doctrine Odm,Doctrine Mongodb,我的目标是让两个不同的文档管理器连接到共享相同数据库模型的不同数据库 我已经对数据库模型进行了重大更改,我想编写一个自定义迁移脚本,从旧模型检索对象,读取其值,然后在新模式上使用旧对象的信息创建一个新对象 我在这里发现了一个相关的stackoverflow问题: 但是,此解决方案建议对每个数据库使用不同的前缀,并将数据模型的类存储在不同的文件夹中: doctrine: dbal: default_connection: default connections:
doctrine:
dbal:
default_connection: default
connections:
default:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
charset: UTF8
second:
driver: %database_sqlite_driver%
host: ~
port: ~
dbname: %database_sqlite_shop_name%
path: %database_sqlite_shop_name%
user: ~
password: ~
charset: UTF8
orm:
auto_generate_proxy_classes: %kernel.debug%
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
YourBundle:
# you must specify the type
type: "annotation"
# The directory for entity (relative to bundle path)
dir: "Entity/FirstDb"
#the prefix
prefix: "Your\Bundle\Entity\FirstDb"
shop:
connection: second
mappings:
YourBundle:
type: "annotation"
#here the second path where entity for the connection stand
dir: "Entity/SecondDb"
#the prefix
prefix: "Your\Bundle\Entity\SecondDb"
我真的希望有两个不同的document manager对象,它们共享同一文件夹中的相同模型,但连接到不同的数据库。这可能吗 我发现在同一个Symfony包中连接到不同的数据库确实是可能的。这个答案让我找到了一个可能的解决方案: 这个简单的php mongodb驱动程序和doctrine mongodb odm之间的唯一区别在于,这个驱动程序的查询结果是关联数组,doctrine mongodb odm diver的结果是对象
我希望这些信息对某人有用 尝试将相同的文档映射到两个
DocumentManager
s时是否出现错误?是的,文档管理员不再检索任何文档。我查询的每个文档都没有。我假设这可能与此配置创建的某些冲突有关。请尝试修改这些冲突。对于mongo:和MySQL:
#services.yml
acme_app.dynamic_connection:
class: %acme.dynamic_doctrine_connection.class%
calls:
- [setDoctrineConnection, @doctrine.dbal.default_connection]]
<?php
namespace Acme\Bundle\AppBundle;
use Doctrine\DBAL\Connection;
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
use Exception;
class DynamicDoctrineConnection
{
/**
* @var Connection
*/
private $connection;
/**
* Sets the DB Name prefix to use when selecting the database to connect to
*
* @param Connection $connection
* @return SiteDbConnection $this
*/
public function setDoctrineConnection(Connection $connection)
{
$this->connection = $connection;
return $this;
}
public function setUpAppConnection()
{
if ($this->request->attributes->has('appId')) {
$connection = $this->connection;
$params = $this->connection->getParams();
// we also check if the current connection needs to be closed based on various things
// have left that part in for information here
// $appId changed from that in the connection?
// if ($connection->isConnected()) {
// $connection->close();
// }
// Set default DB connection using appId
//$params['host'] = $someHost;
$params['dbname'] = 'Acme_App'.$this->request->attributes->get('appId');
// Set up the parameters for the parent
$connection->__construct(
$params, $connection->getDriver(), $connection->getConfiguration(),
$connection->getEventManager()
);
try {
$connection->connect();
} catch (Exception $e) {
// log and handle exception
}
}
return $this;
}
}
$mongo = new \Mongo('mongodb://localhost:27017');
$legacyDbDocumentMangager = $mongo->selectDB('backed_up_prod_db');
$legacyUserCollection = $legacyDbDocumentMangager->selectCollection('User');
$user = $legacyUserCollection->findOne(array('email' => 'matyas@stackoverflow.com'));