Symfony2 - 如何动态地获得一个Doctrine实体的实体经理 [英] Symfony2 - How to Dynamically get a Doctrine entity's Entity Manager

查看:133
本文介绍了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.

注意:我们有几个entityManagers可以分开对不同数据库的访问,例如全球,帐单,本地等

有很多情况需要检测实体的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管理的捆绑包。但该实体可能位于GlobalEntityManager中(您不能认为它位于同一个实体管理器中)。因此,我们需要检测并持有正确的实体的正确的实体管理员。

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?

So how do you recommend dynamically detecting the entityManager for an entity?

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

这是一个简单的解决方案。但是,我不太了解Symfony和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: Gutensite\CmsBundle\Service\EntityHelper
    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 = \Doctrine\Common\Util\ClassUtils::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);
}

更新10/21/15:实体检测代码每@ 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 = \Doctrine\Common\Util\ClassUtils::getRealClass(get_class($entity))

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

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

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

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