Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listener not called in Console Command

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

like image 960
Stefan Bergfeld Avatar asked Nov 01 '13 19:11

Stefan Bergfeld


1 Answers

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 }
like image 93
Nicolai Fröhlich Avatar answered Oct 22 '22 02:10

Nicolai Fröhlich