有没有办法在 Symfony2 中指定 Doctrine2 Entitymanager 实现类? [英] Is there a way to specify Doctrine2 Entitymanager implementation class in Symfony2?

查看:17
本文介绍了有没有办法在 Symfony2 中指定 Doctrine2 Entitymanager 实现类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 Symfony2 和 Doctrine2,但我必须覆盖 Doctrine2 EntityManager 并向其添加一些取消删除"功能(内部的 ACL).

I'm currently working with Symfony2 and Doctrine2, but I must override the Doctrine2 EntityManager and add it some "undelete" features (ACLs inside).

所以我想知道:有没有办法覆盖 EntityManager 类并在 Symfony2 中指定 Doctrine2 以将其用作 EntityManager 的实现?

So I'm wondering : is there a way to override the EntityManager class and specify Doctrine2 in Symfony2 to use it as implementation of the EntityManager?

感谢您的帮助!

推荐答案

是的,可以通过两个步骤:

Yes, it's possible with two steps:

1 - 覆盖 doctrine.orm.entity_manager.class 参数以指向您的自定义实体管理器(它应该扩展 DoctrineORMEntityManager.)

1 - Override the doctrine.orm.entity_manager.class parameter to point to your custom entity manager (which should extend DoctrineORMEntityManager.)

2 - 您的自定义实体管理器必须覆盖 create 方法,以便它返回您的类的实例.请参阅下面的示例,并注意关于 MyEntityManager 的最后一行:

2 - Your custom entity manager must override the create method so that it returns an instance of your class. See my example below, and note the last line regarding MyEntityManager:

public static function create($conn, Configuration $config, EventManager $eventManager = null) {
        if (!$config->getMetadataDriverImpl()) {
            throw ORMException::missingMappingDriverImpl();
        }

        if (is_array($conn)) {
            $conn = DoctrineDBALDriverManager::getConnection($conn, $config, ($eventManager ? : new EventManager()));
        } else if ($conn instanceof Connection) {
            if ($eventManager !== null && $conn->getEventManager() !== $eventManager) {
                throw ORMException::mismatchedEventManager();
            }
        } else {
            throw new InvalidArgumentException("Invalid argument: " . $conn);
        }

        // This is where you return an instance of your custom class!
        return new MyEntityManager($conn, $config, $conn->getEventManager());
    }

您还需要在课堂中使用以下内容:

You'll also need to use the following in your class:

use DoctrineORMEntityManager;
use DoctrineORMConfiguration;
use DoctrineORMORMException;
use DoctrineCommonEventManager;
use DoctrineDBALConnection;

老实说,我很惊讶完全需要第二步,我认为这应该可以仅使用服务容器来完成.

To be honest, I'm surprised that the 2nd step is required at all, I would think this should be possible to accomplish using only the service container.

这篇关于有没有办法在 Symfony2 中指定 Doctrine2 Entitymanager 实现类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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