Doctrine DBAL: Updating timestamp field with 'NOW()' value

随声附和 提交于 2019-12-13 00:29:13

问题


Using Doctrine DBAL, I have some code that inserts a new row into the main database from a form values binded as $telephone_international and $surname.

After that is done, it inserts a new record into a duplicate database. $app['dbs']['backup']

If that's successful, the entry inserted previously the main database gets its copied value updated. The copied column is a timestamp, default value is 0, but the following code should change it to the current time.

$app['dbs']['main']->update('phonebook', array(
    'mediated'  => 'NOW()'
), array(
    'telephone' => $telephone_international, 
    'surname'   => $surname
));

But the value is still 0000-00-00 00:00:00. I wonder if 'NOW()' is being treated as a string.


回答1:


As i proposed in the comments above, this seems the way to go, as stated here:
http://doctrine-orm.readthedocs.org/en/2.0.x/cookbook/working-with-datetime.html

So try like this:

$app['dbs']['main']->update('phonebook', array(
    'mediated'  => new \DateTime("now")
), array(
    'telephone' => $telephone_international, 
    'surname'   => $surname
));



回答2:


I stumbled over the same problem. As there's no much and no good documentation for DBAL itself, I'm going to post my solution.

There's a last parameter which specifies the type (in the order of the data and identifcation array merged; as if they were in the same array):

$app['dbs']['main']->update('phonebook', array(
    'mediated'  => new DateTime()
), array(
    'telephone' => $telephone_international, 
    'surname'   => $surname
), array(
    'datetime',
    PDO::PARAM_STR,
    PDO::PARAM_STR
));


来源:https://stackoverflow.com/questions/11258858/doctrine-dbal-updating-timestamp-field-with-now-value

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