问题描述
我目前使用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).
'm想知道:有没有办法重写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?
谢谢任何帮助! p>
Thank you for any help!
推荐答案
是的,可以通过两个步骤:
Yes, it's possible with two steps:
doctrine.orm.entity_manager.class
参数指向您的自定义实体管理器(应扩展 Doctrine\ORM\EntityManager
。)
1 - Override the doctrine.orm.entity_manager.class
parameter to point to your custom entity manager (which should extend Doctrine\ORM\EntityManager
.)
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 = \Doctrine\DBAL\DriverManager::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 Doctrine\ORM\EntityManager;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\ORMException;
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Connection;
说实话,我很惊讶,第二步是必需的,我想这个应该可以实现仅使用服务容器。
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实现类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!