我想在我的项目中使用 doctrine ODM,我想为我的每个客户建立一个单独的数据库。我希望能够通过我的 API 在运行时管理客户端。我现在的问题是:
当我设置 doctrine ODM 时,我必须在 parameters.yml 中设置我的数据库设置,但我希望能够在运行时选择数据库。我将拥有一个主数据库,其中包含我的所有灯具集合和客户索引,以了解选择哪个数据库,但客户特定的内容将位于这些客户数据库中。每个 Document 类仍将像正常情况下一样链接到一个集合,但随后会链接到不同的数据库中。
有没有办法在运行时为文档类选择数据库?
假设我去 www.myproject.com/client1/item/list 我将列出 dbclient1.Items 集合中的每个项目,如果我转到 www.myproject.com/client2/item/list,我将列出 dbclient2.Items 集合中的所有项目。
我希望我在这里说清楚了我想达到的目标......我找不到任何关于这个的东西,但我认为如果我是第一个对此有疑问的人会很奇怪......必须有在我之前有人有同样的想法吗?
最佳答案
Is there a way to select the database for a Document class at runtime?
是的,您可以在每次需要更改数据库时调用 $dm->getConfiguration()->setDefaultDb('some_db_name')
,但这可能会在写入开始时导致意外行为。
根据我的经验(以及几年 Multi-Tenancy 应用程序的实践),最可靠的方法是为每个上下文创建单独的 DocumentManager
实例。您可以通过使用您的参数正常配置一个 DocumentManager
来实现这一点,但从不直接使用 - 相反,您需要一个将管理其实例的类,fe:
use Doctrine\ODM\MongoDB\DocumentManager;
class DocumentManagerFactory
{
/**
* DocumentManager created by Symfony.
*
* @var DocumentManager
*/
private $defaultDocumentManager;
/**
* All DocumentManagers created by Factory so far.
*
* @var DocumentManager[]
*/
private $instances = array();
public function __construct(DocumentManager $dm)
{
$this->defaultDocumentManager = $dm;
}
public function createFor(Context $ctx)
{
$databaseName = $ctx->getDatabaseName();
if (isset($this->instances[$databaseName])) {
return $this->instances[$databaseName];
}
$configuration = clone $this->defaultDocumentManager->getConfiguration();
$configuration->setDefaultDB($databaseName);
return $this->instances[$databaseName] = DocumentManager::create(
$this->defaultDocumentManager->getConnection(),
$configuration,
$this->defaultDocumentManager->getEventManager()
);
}
}
由于这种方法,来自少数数据库的文档永远不会由一个 DocumentManager
管理,您有一个类负责干预 ODM 配置和保留的框架方法(每个事件订阅者等都是相同的) DocumentManager
).
关于php - Doctrine ODM 在运行时选择数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42057079/