Symfony + Doctrine Oracle DateTime format issue Symfony + Doctrine Oracle DateTime format issue oracle oracle

Symfony + Doctrine Oracle DateTime format issue


Create a custom DBAL Type extending the DateTimeType and override the convertToPHPValue function (I copied the VarDateTimeType class, which couldn't successfully convert the Date type my Oracle installation was using):

<?phpnamespace YourCompany\SomeBundle\Doctrine\DBAL\Types;use Doctrine\DBAL\Platforms\AbstractPlatform;use Doctrine\DBAL\Types\ConversionException;use Doctrine\DBAL\Types\DateTimeType;class SillyDateTimeType extends DateTimeType{    /**     * {@inheritdoc}     * @throws \Doctrine\DBAL\Types\ConversionException     */    public function convertToPHPValue($value, AbstractPlatform $platform)    {        if ($value === null || $value instanceof \DateTime) {            return $value;        }        $val = \DateTime::createFromFormat('d-M-y H.i.s.u A', $value);        if ( ! $val instanceof \DateTime) {            throw ConversionException::conversionFailed($value, $this->getName());        }        return $val;    }}

Replace $val = \DateTime::createFromFormat('d-M-y H.i.s.u A', $value); with whatever format your installation is returning for those columns.

Then just register it under dbal in config.yml:

# app/config/config.ymldoctrine:    dbal:        types:          sillydatetime: YourCompany\SomeBundle\Doctrine\DBAL\Types\SillyDateTimeType

Now you can use sillydatetime (or whatever you call it) anywhere in your column type specifications.


I fixed the problem following the proposal of this comment in Github. In app/config/config.yml (Symfony 2.3.1) I added the following block:

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


I don't know about Symfony/Doctrine, but this is simple to fix with the regular OCI functions:

$conn = oci_connect('username', 'password', 'connection_string');// get the sysdate...$select = oci_parse($conn, 'select sysdate from dual');oci_execute($select);print_r(oci_fetch_row($select));// alter the session date format...$alter = oci_parse($conn, 'alter session set NLS_DATE_FORMAT=\'YYYY-MM-DD HH24:MI:SS\'');oci_execute($alter);// get the sysdate again...oci_execute($select);print_r(oci_fetch_row($select));

This gives the output:

Array(    [0] => 10-JUN-16)Array(    [0] => 2016-06-10 13:39:34)