我按照这个例子来测试softdeletable
我运行 Symfony 2.1.0-DEV 的项目的扩展。
我配置了我的 config.yml,如下所示:
orm:
auto_generate_proxy_classes: %kernel.debug%
auto_mapping: true
filters:
softdeleteable:
class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
enabled: true
mappings:
translatable:
type: annotation
alias: Gedmo
prefix: Gedmo\Translatable\Entity
# make sure vendor library location is correct
dir: "%kernel.root_dir%/../vendor/gedmo/doctrine-extensions/lib/Gedmo/Translatable/Entity"
loggable:
type: annotation
alias: Gedmo
prefix: Gedmo\Loggable\Entity
dir: "%kernel.root_dir%/../vendor/gedmo/doctrine-extensions/lib/Gedmo/Loggable/Entity"
tree:
type: annotation
alias: Gedmo
prefix: Gedmo\Tree\Entity
dir: "%kernel.root_dir%/../vendor/gedmo/doctrine-extensions/lib/Gedmo/Tree/Entity"
我的控制器动作:
/**
* @Route("/del", name="del_article")
*/
public function delAction() {
$em = $this->getDoctrine()->getEntityManager();
$article = $em->find('Article', 3);
$em->remove($article);
$em->flush();
die('ok');
}
当我运行代码时,它总是显示异常:Listener "SoftDeleteableListener" was not added to the EventManager!
经过一段时间的调试,我发现该类SoftDeleteableFilter
具有功能getListener()
:
protected function getListener()
{
if ($this->listener === null) {
$em = $this->getEntityManager();
$evm = $em->getEventManager();
foreach ($evm->getListeners() as $listeners) {
foreach ($listeners as $listener) {
if ($listener instanceof SoftDeleteableListener) {
$this->listener = $listener;
break 2;
}
}
}
if ($this->listener === null) {
throw new \RuntimeException('Listener "SoftDeleteableListener" was not added to the EventManager!');
}
}
return $this->listener;
}
然而$listeners
属性没有SoftDeleteableListener
项目,但它有其他侦听器,例如
- Gedmo\Tree\TreeListener
- Gedmo\Sortable\SortableListener
- Gedmo\Sluggable\SluggableListener
- Gedmo\Loggable\LoggableListener
- Gedmo\Timestampable\TimestampableListener
- Gedmo\Translatable\TranslatableListener
它们是从 loadClassMetadata 生成的。我认为它可能从我的学说扩展.yml 服务侦听器生成:
services:
extension.listener:
class: Infinitz\UserBundle\Listener\DoctrineExtensionListener
calls:
- [ setContainer, [ @service_container ] ]
tags:
- { name: kernel.event_listener, event: kernel.request, method: onLateKernelRequest, priority: -10 }
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
gedmo.listener.tree:
class: Gedmo\Tree\TreeListener
tags:
- { name: doctrine.event_subscriber, connection: default }
calls:
- [ setAnnotationReader, [ @annotation_reader ] ]
gedmo.listener.translatable:
class: Gedmo\Translatable\TranslatableListener
tags:
- { name: doctrine.event_subscriber, connection: default }
calls:
- [ setAnnotationReader, [ @annotation_reader ] ]
- [ setDefaultLocale, [ %locale% ] ]
- [ setTranslationFallback, [ false ] ]
gedmo.listener.timestampable:
class: Gedmo\Timestampable\TimestampableListener
tags:
- { name: doctrine.event_subscriber, connection: default }
calls:
- [ setAnnotationReader, [ @annotation_reader ] ]
gedmo.listener.sluggable:
class: Gedmo\Sluggable\SluggableListener
tags:
- { name: doctrine.event_subscriber, connection: default }
calls:
- [ setAnnotationReader, [ @annotation_reader ] ]
gedmo.listener.sortable:
class: Gedmo\Sortable\SortableListener
tags:
- { name: doctrine.event_subscriber, connection: default }
calls:
- [ setAnnotationReader, [ @annotation_reader ] ]
gedmo.listener.loggable:
class: Gedmo\Loggable\LoggableListener
tags:
- { name: doctrine.event_subscriber, connection: default }
calls:
- [ setAnnotationReader, [ @annotation_reader ] ]
所以我尝试添加以下内容:
gedmo.listener.softdeleteable:
class: Gedmo\SoftDeleteable\SoftDeleteableListener
tags:
- { name: doctrine.event_subscriber, connection: default }
calls:
- [ setAnnotationReader, [ @annotation_reader ] ]
但它仍然显示Listener "SoftDeleteableListener" was not added to the EventManager!
我需要在哪个 SoftDeleteableListener 实例中添加监听器?