doctrine 2 oracle datetme column showing “not a va

2019-05-11 09:48发布

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

2条回答
Deceive 欺骗
2楼-- · 2019-05-11 10:13

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.

查看更多
在下西门庆
3楼-- · 2019-05-11 10:16

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/

查看更多
登录 后发表回答