doctrine 2 oracle datetime column showing “not a valid month” on insert entity

独自空忆成欢 提交于 2019-12-10 10:04:57

问题


I am working on a project where we are using Zend 2 and doctrine 2 with oracle database. my entity has a field create_date with datetime type. my entity are below

class Personnel
{

/**
 * @ORM\Column(type="string",unique=true, nullable=false)
 */
protected $login_name;
/**
 * @ORM\Column(type="datetime")
 */
protected $create_date;
public function __construct()
{
    $this->create_date = new \DateTime("now");
}

 public function get_login_name()
{
    return $this->login_name;
}

public function set_login_name($login_name)
{
    $this->login_name = $login_name;
}

}

and im saving this entity with

$user = new Personnel();
$user->set_login_name('Admin');
$this->getEntityManager()->persist($user);
$this->getEntityManager()->flush();

but showng an error

    SQLSTATE[HY000]: General error: 1843 OCIStmtExecute: ORA-01843: not a valid month
     (ext\pdo_oci\oci_statement.c:148)

Please Help me.

advanced thx


回答1:


Found that bug here too... it's OracleSessionInit not being called!

But i dunno if it's a missing config part or if it's a bug from doctrine not enabling that by default if you use oci8

edit:

just found it! and i should add that you should add a service in invokers at service_manager pointing to \Doctrine\DBAL\Event\Listeners\OracleSessionInit, so it should be something like this:

'invokables' => array(
    'oracle-session-init' => '\Doctrine\DBAL\Event\Listeners\OracleSessionInit'
),

and this:

'doctrine' => array (
    'driver' => array (
            /** here are your driver settings, such as annotations configs */
    ),
    'eventmanager' => array(
            'orm_default' => array(
                    'subscribers' => array('oracle-session-init')
            )
    )
),

credits to: http://raymondkolbe.com/2012/06/19/doctrineormmodule-and-oraclesessioninit/




回答2:


Doctrine doesn't call it by default, even with you use the OCI8. In my opnion, it's not a bug because with you can change the NLS params directly in DB, for good, you won't need to call every time you connect the OracleSessionInit. Resulting in 1 query less per session. :)

Another way to solve this it's getting the instances of \Doctrine\ORM\EntityManager and setting the OracleSessionInit, like below. You can also change the default session vars like I did in this example passing an array with the new value to 'NLS_SORT'.

At the Module.php:

public function getServiceConfig() {
    return array(
        'initializers' => array(
            function($instance, $services) {
                if ($instance instanceof \Doctrine\ORM\EntityManager) {
                    $instance->getEventManager()->addEventSubscriber(new \Doctrine\DBAL\Event\Listeners\OracleSessionInit(array(
                        'NLS_SORT' => 'WEST_EUROPEAN_AI',
                    )));
                }
            },
        ),
    );
}

Initializer: A callback that is executed every time the ServiceManager creates a new instance of a class. These are usually used to inject an object into the new class instance if that class implements a particular interface.

More about initializer and Zend\ServiceManager config here.



来源:https://stackoverflow.com/questions/14930670/doctrine-2-oracle-datetime-column-showing-not-a-valid-month-on-insert-entity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!