我的目标是让两个不同的文档管理器连接到共享相同数据库模型的不同数据库。
我已经对数据库模型进行了很大的更改,我想编写一个自定义迁移脚本,该脚本从旧模型中检索对象并读取其值,然后使用旧对象的信息在新架构上创建一个新对象。
我在这里发现了一个相关的stackoverflow问题:在Symfony2的同一捆绑中使用两个实体管理器
但是,该解决方案建议为每个数据库使用不同的前缀,并将数据模型的类存储在不同的文件夹中:
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"
我真的很想拥有两个不同的文档管理器对象,它们在同一文件夹中共享相同的模型,但是连接到不同的数据库。这可能吗?
我发现确实可以在同一Symfony捆绑包中连接到不同的数据库。这个答案使我想到了一个可能的解决方案:https : //stackoverflow.com/a/15110867/2174832
#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;
}
}
通过上述解决方案,可以编写一项服务,可以将其更改为将当前实体管理器连接到的数据库更改为其他数据库的服务。
不幸的是,上述解决方案仅适用于PDO驱动程序(Mysql)。由于我们的技术堆栈包括mongodb,并且我们使用了doctrine-mongodb捆绑包,因此我不得不寻找不同的解决方案。
doctrine-mongodb文档在此处提供了有关设置自定义文档管理器的部分:http : //docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/introduction.html
我无法获得正确的doctrine-mongodb odm文档映射来与文档中的说明一起使用,因此对我而言,唯一的解决方案是创建一个简单的PHP mongoclient连接:
$mongo = new \Mongo('mongodb://localhost:27017');
$legacyDbDocumentMangager = $mongo->selectDB('backed_up_prod_db');
$legacyUserCollection = $legacyDbDocumentMangager->selectCollection('User');
$user = $legacyUserCollection->findOne(array('email' => '[email protected]'));
这个简单的php mongodb驱动程序和doctrine-mongodb odm之间的唯一区别是,该驱动程序的查询结果是关联数组,而doctrine-mongodb odm diver的结果是对象。
我希望这些信息对某人有用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句