Search code examples
phpexceptiondoctrine-ormoracle11gzend-framework2

"literal does not match format string" when saving dates using Doctrine ORM & Zend2 & Oracle


When I try to save Date or DateTime fields using Doctrine ORM, and specifically in Zend2+Oracle, I get the following error:

An exception occurred while executing 'INSERT INTO NEWS (ID, TITLE, SLUG, CONTENT, ACTIVE, PUBLISH_DATE, CATEGORY_ID) VALUES (?, ?, ?, ?, ?, ?, ?)' with params [23, "some title", "some-slug", "some content", "1", "2016-06-18 00:00:00", 2]:

ORA-01861: literal does not match format string

After some search, I have found that the reason behind this is most probably a bug in the ORM, the OracleSessionInit does not correctly initialize when working with Oracle, and thus the NLS or other environment settings does not be loaded, this means that the date is not being treated correctly.


Solution

  • I have got it fixed by replacing the line

    return EntityManager::create($connection, $config);
    

    in the file: \vendor\doctrine\doctrine-orm-module\src\DoctrineORMModule\Service\EntityManageFactory.php, under the Function createService.php with the following lines:

    $em = EntityManager::create($connection, $config);
    $dbh = $em->getConnection();
    $sth = $dbh->prepare("ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS'");
    $sth->execute();
    return $em;