SYMFONY 4实体DateTime字段返回当前日期而不是数据库。
我有一个实体图像,该图像将更新日期存储在称为UpdatedAt的字段中。
<?php /** @noinspection PhpFullyQualifiedNameUsageInspection */
namespace AppEntity;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;
use SymfonyComponentHttpFoundationFileFile;
use VichUploaderBundleMappingAnnotation as Vich;
/**
* @ORMEntity(repositoryClass="AppRepositoryImageRepository")
* @VichUploadable
*/
class Image
{
/**
* @ORMId()
* @ORMGeneratedValue()
* @ORMColumn(type="integer")
*/
private $id;
/**
* @var File
* @VichUploadableField(mapping="images", fileNameProperty="name")
*/
private $file;
/**
* @var string
* @ORMColumn(type="string", length=255)"
*/
private $name;
/**
* @var DateTime
* @ORMColumn(type="datetime")
*/
private $updatedAt;
/**
* @ORMManyToMany(targetEntity="AppEntityTag", inversedBy="images", cascade={"persist"})
*/
private $tags;
public function __construct()
{
$this->updatedAt = new DateTime();
$this->tags = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getFile(): ?File
{
return $this->file;
}
public function setFile(?File $file = null): void
{
$this->file = $file;
if ($file !== null) {
// It is required that at least one field changes if you are using doctrine
// otherwise the event listeners won't be called and the file is lost
$this->updatedAt = new DateTime();
}
}
public function getName(): ?string
{
return $this->name;
}
public function setName(?string $name): self
{
$this->name = $name;
return $this;
}
public function getUpdatedAt(): ?DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(DateTimeInterface $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
}
但是,当我从数据库中检索它时,更新的字段将当前的数据显示为bellow:
Image {#646 ▼
-id: 27
-file: File {#698 ▶}
-name: "kPRHHeCVrT.jpg"
-updatedAt: DateTime @1558609804 {#706 ▼
date: 2019-05-23 11:10:04.486082 UTC (+00:00)
}
-tags: PersistentCollection {#686 ▶}
}
和getupdatedat方法中的var_dump($this->updatedAt)
显示了以下内容:
object(DateTime)#626 (3) { ["date"]=> string(26) "2019-05-19 21:17:48.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" }
object(DateTime)#680 (3) { ["date"]=> string(26) "2019-05-23 11:24:12.746548" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" }
注意2个不同的对象。我不明白为什么这样做。
作为您的代码状态中的评论(您将其保存在官方的Vichuploaderbundle文档中(:
public function setFile(?File $file = null): void
{
$this->file = $file;
if ($file !== null) {
// It is required that at least one field changes if you are using doctrine
// otherwise the event listeners won't be called and the file is lost
$this->updatedAt = new DateTime();
}
}
因此,这意味着每当您调用 setFile((方法时,您都会将当前日期分配给您的 $ this-> updateDateT 属性。每当您创建/更新图像对象时,该方法都会调用,因此它始终保持当前日期。这也可能发生,因为Ingect_on_on_load参数设置为VICH CONFIG YAML文件中的true。
我建议您添加一个名为vichupdatedat((的新属性,然后使用它代替更新((:
- VichupDatedAt将由捆绑包使用。
- 更新AT将由其他所有内容使用。
奖励:我鼓励您安装这些扩展名:https://symfony.com/doc/current/bundles/stofdoctrineextensionsbundle/index.html它将允许您添加udpatedat&amp;创建的属性都可以轻松。
激活您想要的扩展名( in/config/packages/stof_doctrine_extensions.yaml (,因此,对于我们来说,这将是时间流程:
stof_doctrine_extensions:
default_locale: en_US
orm:
default:
timestampable: true
然后在您的图像类中,只需添加:
use GedmoTimestampableTraitsTimestampableEntity;
class Image {
use TimestampableEntity;
....
}
,您去了,免费更新&amp;创建的属性,别无其他,它只是工作。