Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Doctrine OracleSessionInit listener with Symfony2?

I am using oracle and I am recieving this error:

Could not convert database value "17-NOV-11 12.17.33 AM" to Doctrine Type datetime. Expected format: Y-m-d H:i:s

Also I can´t create new rows because oracle complaints about the datetime format.

I know that mysql datetime format and oracle one are different.

I understand that you can use doctrine listeners with symfony 2:

http://symfony.com/doc/current/cookbook/doctrine/event_listeners_subscribers.html

And, there´s a doctrine listener created to fix this issue:

Doctrine/DBAL/Event/Listeners/OracleSessionInit.php

The question is how Exactly your turn it on in symfony 2.

like image 266
Mauricio Lopez Avatar asked Nov 17 '11 13:11

Mauricio Lopez


2 Answers

Well It appears I will answer myself this time.

You have to add it as a service using the event tag.

app/config/config.yml 

services:
    my.listener:
        class: Doctrine\DBAL\Event\Listeners\OracleSessionInit 
        tags:
            - { name: doctrine.event_listener, event: postConnect }

my.listener is an arbitrary name for the listener.

like image 162
Mauricio Lopez Avatar answered Oct 19 '22 10:10

Mauricio Lopez


In my case Oracle was a second connection (stored under oracle key). The application also used PostgreSQL (stored under default key).

The problem was that Listener was executed on the default database (PostgreSQL). So I changed code to:

services:
    my.oracle.listener:
        class: Doctrine\DBAL\Event\Listeners\OracleSessionInit
        tags:
            - { name: doctrine.event_listener, event: postConnect, connection: oracle }

And all works fine!

like image 22
Tomasz Kuter Avatar answered Oct 19 '22 09:10

Tomasz Kuter