简体   繁体   中英

ON UPDATE MySQL with doctrine DBAL

I use doctrine DBAL 2.5.2 with MySql 5.5

I have created a field with timestamp type and an ON UPDATE CURRENT_TIMESTAMP extra.

When I change the content from phpMyAdmin the field is updated and change to the current timestamp, but when I update the content from Doctrine/dbal nothing happen. I mean everything works except the on update .

I call the update with $app['db']->update('mytable', $data, array('my_id' => $id);

How I can run the on update trigger from the doctrine/dbal code ?

edit :

I add this code to check if I try to change the date.

if (isset($data['updated'])) unset($data['updated']);
  1. Make sure you don't send the date field with your $data at all as an empty string will result in zeroes.
  2. Make sure you're actually changing data, otherwise, the timestamp column won't get updated too.

I have set up a test project, it works fine:

$connectionParams = ...;
$conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);
$conn->update('user', array('name' => 'X3'), array('name' => 'X2'));

Table:

CREATE TABLE `user` (
  `id` int(11) NOT NULL,
  `name` text NOT NULL,
  `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Some test data:

INSERT INTO `user` (`id`, `name`, `date`) VALUES
(1, 'A', '0000-00-00 00:00:00'),
(2, 'B', '0000-00-00 00:00:00'),
(3, 'C', '0000-00-00 00:00:00'),
(4, 'F', '0000-00-00 00:00:00'),
(10, 'X2', '2015-11-10 14:47:08');

Can you retest and make sure you have all the parameters correct on the date field?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM