之前没有发生这种情况,但我只是注意到我所有的虚拟夹具信息(帖子)都显示出创建时间比实际时间提前一小时?
当我手动输入帖子时,时间是正确的。
知道造成这种情况的原因是什么?
我正在为我的Post实体扩展的createdAt和updatedAt使用Timestampable实体文件。 Timestampable实体文件使用stof / doctrine-extensions-bundle。
时间戳实体
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* Timestampable abstract class to define created and updated behavior
*
* @ORM\MappedSuperclass
*/
abstract class Timestampable
{
/**
* @var \DateTime
*
* @Gedmo\Timestampable(on="create")
* @ORM\Column(name="created_at", type="datetime")
*/
private $createdAt;
/**
* @var \DateTime
*
* @Gedmo\Timestampable(on="update")
* @ORM\Column(name="updated_at", type="datetime", nullable=true)
*/
private $updatedAt;
/**
* Set createdAt
*
* @param \DateTime $createdAt
* @return Timestampable
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Get createdAt
*
* @return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Set updatedAt
*
* @param \DateTime $updatedAt
* @return Timestampable
*/
public function setUpdatedAt($updatedAt)
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* Get updatedAt
*
* @return \DateTime
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
}
帖子夹具
public function load(ObjectManager $manager)
{
$post1 = new Post();
$post1->setCategory($this->getReference('category-1'));
$post1->addReply($this->getReference('reply-1'));
$post1->addReply($this->getReference('reply-2'));
$post1->addReply($this->getReference('reply-3'));
$post1->setTitle('Lorem Ipsum 1');
$post1->setAuthor('Foo1');
$post1->setBody('Lorem ipsum dolor sit amet, consectetur adipiscing elit.)
}
答案 0 :(得分:1)
您认为时区与Timestampable
存在问题。您的灯具数据存储在UTC
时区。
请参阅此相关文章(阅读有关时区的评论):
Symfony2 datetime best way to store timestamps?
上面的这篇文章提供了一个有趣的博客条目的链接,可以帮助您找到最适合您的场景的解决方案:
https://matt.drollette.com/2012/07/user-specific-timezones-with-symfony2-and-twig-extensions/
编辑:
您可以使用LifecycleCallback
使用显式时区设置创建的值。
/**
* @ORM\Entity
* @ORM\HasLifecycleCallbacks()
**/
/*
...
*/
/**
* @ORM\PrePersist
*/
public function setCreatedValue()
{
$now = new \DateTime('now' , new \DateTimeZone('Europe/Berlin') );
$this->setCreated( $now );
}