在 Entity 中获取 entityManager [英] Get entityManager inside an Entity

查看:46
本文介绍了在 Entity 中获取 entityManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用,比如:

$em = $this->getEntityManager();

在实体内部.

我知道我应该将其作为一项服务进行,但出于某些测试目的,我想从实体访问它.

I understand I should do this as a service but for some testing purposes, I want to access it from an Entity.

有可能实现吗?

我已经尝试过:

$em = $this->getEntityManager();
$profile_avatar = $em->getRepository('bundle:Perfils')->findOneByUser($this-getId());

但不工作.

致命错误:调用未定义的方法代理webBundleEntityUserProxy::getEntityManager() 在/opt/lampp/htdocs/web/src/Pct/bundle/Entity/User.php上线449

Fatal error: Call to undefined method ProxieswebBundleEntityUserProxy::getEntityManager() in /opt/lampp/htdocs/web/src/Pct/bundle/Entity/User.php on line 449

我为什么要这样做?

我有 3 种用户:Facebook、Twitter 和 MyOwnWebsite 用户.他们每个人都有不同的头像,链接 facebook 的个人资料、twitter 或其他,如果它的 myownwebsite 用户,我从数据库中的 URL 检索头像.现在,我不想创建服务,因为我只是想让它工作,测试它,而不是创建最终部署.所以这就是我尝试从实体调用实体管理器的原因.我现在不想修改配置文件,只想修改这个实体.

I've 3 kinds of users: Facebook, Twitter and MyOwnWebsite users. Each of them have differents avatar which links facebook's profile, twitter's or otherwise, if its myownwebsite user, I retrieve the avatar from a URL in a database. For now, I don't want to create a service, because I'm just trying to make it working, to test it, not to create a final deployment. So this is why I'm trying to call Entity manager from an Entity. I don't want, by now, to modify configuration files, just this entity.

推荐答案

正如评论者(再次)指出的那样,实体内部的实体管理器是代码异味.对于 OP 的特定情况,他希望以最少的麻烦获得实体管理器,一个简单的 setter 注入将是最可靠的(与我最初通过构造函数注入的示例相反).

As pointed out (again) by a commenter, an entity manager inside an entity is a code smell. For the OP's specific situation where he wished to acquire the entity manager, with the least bother, a simple setter injection would be most reliable (contrary to my original example injecting via constructor).

对于最终在这里寻找解决同一问题的卓越解决方案的其他人,有两种方法可以实现这一目标:

For anyone else ending up here looking for a superior solution to the same problem, there are 2 ways to achieve this:

  1. 按照 https://stackoverflow.com/a/的建议实现 ObjectManagerAware 接口24766285/1349295

  1. Implementing the ObjectManagerAware interface as suggested by https://stackoverflow.com/a/24766285/1349295

use DoctrineCommonPersistenceObjectManagerAware;
use DoctrineCommonPersistenceObjectManager;
use DoctrineCommonPersistenceMappingClassMetadata;
use DoctrineORMMapping as ORM;

/**
 * @ORMEntity
 */
class Entity implements ObjectManagerAware
{
    public function injectObjectManager(
        ObjectManager $objectManager,
        ClassMetadata $classMetadata
    ) {
        $this->em = $objectManager;
    }
}

  • 或者,使用 @postLoad/@postPersist 生命周期回调并按照建议使用 LifecycleEventArgs 参数获取实体管理器作者 https://stackoverflow.com/a/23793897/1349295

  • Or, using the @postLoad/@postPersist life cycle callbacks and acquiring the entity manager using the LifecycleEventArgs argument as suggested by https://stackoverflow.com/a/23793897/1349295

    use DoctrineCommonPersistenceEventLifecycleEventArgs;
    use DoctrineORMMapping as ORM;
    
    /**
     * @ORMEntity
     * @ORMHasLifecycleCallbacks()
     */
    class Entity
    {
        /**
         * @ORMPostLoad
         * @ORMPostPersist
         */
        public function fetchEntityManager(LifecycleEventArgs $args)
        {
            $this->setEntityManager($args->getEntityManager());
        }
    }
    

  • 原答案

    Entity 中使用 EntityManager 是非常糟糕的做法.这样做违背了将查询和持久化操作与实体本身分离的目的.

    Original answer

    Using an EntityManager from within an Entity is VERY BAD PRACTICE. Doing so defeats the purpose of decoupling query and persist operations from the entity itself.

    但是,如果你真的,真的,真的需要一个实体中的实体管理器,并且不能这样做,那么将它注入到实体中.

    But, if you really, really, really need an entity manager in an entity and cannot do otherwise then inject it into the entity.

    class Entity
    {
        private $em;
    
        public function __contruct($em)
        {
            $this->em = $em;
        }
    }
    

    然后调用 new Entity($em).

    这篇关于在 Entity 中获取 entityManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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