Reputation: 201
I followed the this example to test softdeletable
extension on my project running Symfony 2.1.0-DEV.
I configured my config.yml like below:
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"
My controller action:
/**
* @Route("/del", name="del_article")
*/
public function delAction() {
$em = $this->getDoctrine()->getEntityManager();
$article = $em->find('Article', 3);
$em->remove($article);
$em->flush();
die('ok');
}
When I run the code, it always show the exception: Listener "SoftDeleteableListener" was not added to the EventManager!
After some time spent with debugging, I found that the class SoftDeleteableFilter
has function 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;
}
However $listeners
property has no SoftDeleteableListener
item, but it has other listeners, such as
Which are generated from loadClassMetadata. I think it might generate from my doctrine_extensions.yml service listener:
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 ] ]
So I tried to add following:
gedmo.listener.softdeleteable:
class: Gedmo\SoftDeleteable\SoftDeleteableListener
tags:
- { name: doctrine.event_subscriber, connection: default }
calls:
- [ setAnnotationReader, [ @annotation_reader ] ]
But it still shows Listener "SoftDeleteableListener" was not added to the EventManager!
Do I need to add an listener which instance of SoftDeleteableListener?
Upvotes: 18
Views: 16403
Reputation: 43441
As of Symfony 7 and Doctrine 3 this setup works for me:
# packages/doctrine.yaml
doctrine:
orm:
filters:
softdeleteable:
class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
enabled: true
# services.yaml
Gedmo\Mapping\Driver\AttributeReader: ~
Gedmo\SoftDeleteable\SoftDeleteableListener:
class: Gedmo\SoftDeleteable\SoftDeleteableListener
tags:
- {name: doctrine.event_listener, event: 'onFlush'}
- {name: doctrine.event_listener, event: 'loadClassMetadata'}
calls:
- setAnnotationReader: ['@Gedmo\Mapping\Driver\AttributeReader']
Upvotes: 0
Reputation: 21901
If you are on api-platform
This is my config/packages/doctrine.yml
doctrine:
dbal:
url: '%env(resolve:DATABASE_URL)%'
# IMPORTANT: You MUST configure your server version,
# either here or in the DATABASE_URL env var (see .env file)
server_version: '12'
orm:
auto_generate_proxy_classes: true
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
auto_mapping: true
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
filters:
softdeleteable:
class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
enabled: true
after that add the new service to config.services.yml
services:
..........
..........
gedmo.listener.softdeleteable:
class: Gedmo\SoftDeleteable\SoftDeleteableListener
tags:
- { name: doctrine.event_subscriber, connection: default }
calls:
- [ setAnnotationReader, [ '@annotation_reader' ] ]
Finally add the annotation to the entity
use Gedmo\Mapping\Annotation as Gedmo;
/**
* @ApiResource()
* @ORM\Entity(repositoryClass=StoreRepository::class)
* @ORM\Table(name="trn_stores")
* @Gedmo\SoftDeleteable(fieldName="deletedAt")
*/
class Store ...
That's it
Upvotes: 4
Reputation: 790
Couldn't solve problem because of unclear answer.
To add softdeletable behaviour to your project add following lines to your config.yml
orm
..
filters:
softdeleteable:
class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
enabled: true
services:
..
gedmo.listener.softdeleteable:
class: Gedmo\SoftDeleteable\SoftDeleteableListener
tags:
- { name: doctrine.event_subscriber, connection: default }
calls:
- [ setAnnotationReader, [ '@annotation_reader' ] ]
Btw, more complete discussion, which helped me, can be found: https://github.com/Atlantic18/DoctrineExtensions/issues/380
Upvotes: 30
Reputation: 9966
Add the following to your config.yml to activate the softdelete listener:
# app/config.yml
stof_doctrine_extensions:
default_locale: %locale%
orm:
default:
softdeleteable: true
Upvotes: 16
Reputation: 201
sorry about my carelessness, because I overwrote my configuration in config.yml file at the bottom of the file use:
services:
gedmo.listener.softdeleteable:
class: Gedmo\SoftDeleteable\SoftDeleteableListener
and did not configure properly..... now the problem has been fixed.
Upvotes: 0