Symfony2 - 如何动态获取 Doctrine 实体的实体管理器 [英] Symfony2 - How to Dynamically get a Doctrine entity's Entity Manager

查看:22
本文介绍了Symfony2 - 如何动态获取 Doctrine 实体的实体管理器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在构建一个 CMS 和网站建设平台,其中包含许多不同的供应商和客户站点,因此有许多不同的内容类型实体,这些实体由通用控制器和辅助服务编辑,这些实体不一定(容易)知道给定实体的实体管理器是什么.

We are building a CMS and website building platform with a lot of different vendors and clients sites, so there are a lot of different content type entities, which are edited by generic controllers and helper services that don't necessarily (easily) know what the entity manager is for a given entity.

注意:我们有几个 entityManager 来分离对不同数据库的访问,例如全球、计费、本地等

很多情况下我们需要检测实体的EntityManager是什么.例如,我们有一个 MediaHelper,它动态地将数据库中的媒体与实体上的匹配字段相关联(这不适用于关联,因为媒体必须与任何实体连接,而您不能拥有那种动态关联和我们不想要一百个不同的关联).

There are many cases where we need to detect what is the entity's EntityManager. For example, we have a MediaHelper that dynamically associates media from a database with matching fields on the entity (this doesn't work with associations because the Media has to connect with literally any entity and you can't have that kind of dynamic association and we don't want a hundred different associations).

媒体位于由本地"EntityManager 管理的包中.但是实体可能在全局"EntityManager 中(您不能假设它在同一个实体管理器中).所以我们需要为正确的实体检测并持久化正确的实体管理器.

The media is in a bundle managed by the 'Local' EntityManager. But the entity may be in a 'Global' EntityManager (you can't assume it's in the same entity manager). So we need to detect and persist the right entity manager for the right entity.

那么您建议如何动态检测实体的 entityManager?

注意:接受的答案是一个更好的解决方案.这只是为了存档目的.

这是一个有效的简单解决方案.但我对 Symfony 和 Doctrine 的了解还不够,不知道这是否是一个坏主意?还有人知道吗?如果没有,我不知道为什么这不会作为 Doctrine 实用程序出现在核心中.

Here is a simple solution that works. But I don't know enough about Symfony and Doctrine to know if it's a bad idea? Does anyone else know? If not, I don't know why this wouldn't be in the core, as a Doctrine Utility.

我创建了一个 EntityHelper 服务,将 Doctrine 服务注入其中:

I created an EntityHelper service that injects the Doctrine service into it:

gutensite_cms.entity_helper:
    class: GutensiteCmsBundleServiceEntityHelper
    arguments:
        - "@doctrine"

然后在实体助手中有一个简单的函数来获取实体的实体管理器(config.yml 已经为实体管理器注册了包):

Then in the entity helper is one simple function to get the Entity Manager for an entity (the config.yml registers the bundles for entity managers already):

/**
 * Automagically find the entityManager for an entity. 
 * @param $entity
 * @return mixed
 */
public function getManagerForEntity($entity) {
    $className = DoctrineCommonUtilClassUtils::getRealClass(get_class($entity));
    foreach (array_keys($this->doctrine->getManagers()) as $name) {
        if(in_array($className, $this->doctrine->getManager($name)->getConfiguration()->getMetadataDriverImpl()->getAllClassNames())) return $em;
    }
}

注意:Doctrine Registry#getAliasNamespace 已经做了一些事情与这个 foreach 循环几乎相同,我只是修改了返回实体管理器而不是命名空间的想法,例如

NOTE: Doctrine Registry#getAliasNamespace already does something nearly identical to this foreach loop, I just modified the idea to return the entity manager instead of the namespace, e.g.

public function getAliasNamespace($alias) {
    foreach (array_keys($this->getManagers()) as $name) {
        try {
            return $this->getManager($name)->getConfiguration()->getEntityNamespace($alias);
        } catch (ORMException $e) {
        }
    }
    throw ORMException::unknownEntityNamespace($alias);
}

2015 年 10 月 21 日更新:根据 @Cerad 的建议更新了实体检测代码.

推荐答案

根据@qooplmao 的建议,Doctrine 核心中已经有一个简单的方法.

Per the suggestion of @qooplmao, there is an easy method already in the Doctrine core.

// 1) get the real class for the entity with the Doctrine Utility.
$class = DoctrineCommonUtilClassUtils::getRealClass(get_class($entity))

// 2) get the manager for that class. 
$entityManager = $this->container->get('doctrine')->getManagerForClass($class);

根据 Cerad 和 Qooplmao 的建议于 10/22/15 更新

这篇关于Symfony2 - 如何动态获取 Doctrine 实体的实体管理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆