symfony 4 fixtures datetime 方法不使用来自 PDO 数据的字符串总是设置 "now"

symfony 4 fixtures datetime method not working with string from PDO data always set "now"

我尝试使用 Symfony Doctrine Fixtures 将一些数据从 WordPress 迁移到我的 Symfony 应用程序。

除了从 wordpress mysql 数据库列 'post_date' 迁移日期外,一切正常。

例如,我来自 mysql 的变量如下所示:$post_value['post_date'] = '2018-03-30 21:05:24'; 并使用 PDO 拍摄。这个字符串是真实的,我只是 copied/pasted 在这里 ;)

然后我在 DataFixtures 中得到了这样的东西:

$item = new Articles();
$item->setDateCreated(new \DateTime($post_value['post_date']));
...
$this->manager->persist($item);
...
$this->manager->flush();

在数据库中,它不放置迁移值,而是放置类似于使用 \DateTime("now")

的当前日期时间

我的 mysql 结果是 (new/another database.table.column):

SELECT distinct(date_created) FROM articles;

'2019-12-12 22:37:01'
'2019-12-12 22:37:02'
'2019-12-12 22:37:03'
'2019-12-12 22:37:04'
'2019-12-12 22:37:05'
'2019-12-12 22:37:06'

编辑:

这两个结果来自 sql 不同的结果来自迁移所有数据和上面的示例涉及一个条目

EDIT2:

//hard print_r for fisrt row

echo "\n ---- \n >>>";
print_r($post_value['post_date']);
echo "<<< \n ---- \n ";
die();

//result with this
 ----
 >>>2010-05-25 23:05:20<<<
 ----

所以值不为空

我认为问题不在于这段代码,而在于实体。

两种可能:

  1. 实体在保存时覆盖日期时间值。
  2. 该列配置为自动添加 "NOW()" 函数而不是变量值。

然后,要准确了解发生了什么,您可以使用其中的一些线索:

  1. 检查您的实体是否使用 Doctrine Lifecycle。如果是这样,请检查在 @Prepersist 方法期间未覆盖此日期时间值。
  2. 该列应仅注释为:@Column(type="datetime")
  3. 检查你是否在你的实体中不使用任何 Doctrine Timestampable bundle/traits。