5

I needed to persist additional entities when other entities are persisted or updated. Therefore I created a solution inspired from this post: https://stackoverflow.com/a/11054101/1526162.

config.yml:

services:
    transaktion.chain:
        class: Foo\BarBundle\Listener\Chain

    transaktion.flush:
        class: Foo\BarBundle\Listener\Flush
        arguments: [ @doctrine.orm.entity_manager, @transaktion.chain ]
        tags:
            - { name: kernel.event_listener, event: kernel.response, method: onResponse, priority: 5 }

    transaktion.listener:
        class: Foo\BarBundle\Listener\TransaktionLogger
        arguments: [ @transaktion.chain ]
        tags:
            - { name: doctrine.event_listener, event: postPersist }
            - { name: doctrine.event_listener, event: postUpdate }
            - { name: doctrine.event_listener, event: preRemove }

The postPersist, postUpdate and preRemove events are adding information to the Chain and at the end, the kernel.response starts the Flush and the needed additional entites are created. Everything works fine.

But, when I persist entites inside a Console Command it is not working. I think there is no kernel.response event. Is there an other useful event that is working with Controllers and in Console Commands?

Additional info: I am using Symfony 2.3

Community
  • 1
  • 1
Stefan Bergfeld
  • 113
  • 2
  • 9

1 Answers1

9

Console events have been added in Symfony 2.3.

A quick introduction can be found in this blog post.

You can find the console event names in the class Symfony\Component\Console\ConsoleEvents.

const ConsoleEvents::COMMAND = 'console.command';
const ConsoleEvents::TERMINATE = 'console.terminate';
const ConsoleEvents::EXCEPTION = 'console.exception';

Just add the console.terminate tag to your subscriber and you should be fine.

tags:
    - { name: kernel.event_listener, event: kernel.response, method: onResponse }  
    - { name: kernel.event_listener, event: console.terminate, method: onResponse }
Matthew Blackford
  • 3,041
  • 1
  • 24
  • 28
Nicolai Fröhlich
  • 51,330
  • 11
  • 126
  • 130
  • I already worked on that, but I failed finding the right event name for the tags entry. Btw: Is there a list of all available events in the documentation? – Stefan Bergfeld Nov 02 '13 at 17:54