Symfony 4实体datetime字段返回当前日期而不是数据库

时间:2019-05-23 11:31:03

标签: php symfony

Symfony 4实体datetime字段返回当前日期,而不是数据库。

我有一个实体图像,该图像将更新日期存储在一个名为updatedAt的字段中。

    <?php /** @noinspection PhpFullyQualifiedNameUsageInspection */

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;

/**
 * @ORM\Entity(repositoryClass="App\Repository\ImageRepository")
 * @Vich\Uploadable
 */
class Image
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @var File
     * @Vich\UploadableField(mapping="images", fileNameProperty="name")
     */
    private $file;

    /**
     * @var string
     * @ORM\Column(type="string", length=255)"
     */
    private $name;

    /**
     * @var \DateTime
     * @ORM\Column(type="datetime")
     */
    private $updatedAt;

    /**
     * @ORM\ManyToMany(targetEntity="App\Entity\Tag", 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;
    }


}

但是当我从数据库中检索到它时,updatedAt字段将当前数据时间显示为以下情况:

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个不同的对象。我不明白为什么会这么做。

1 个答案:

答案 0 :(得分:2)

代码中的注释说明了(如您保留在官方的VichUploaderBundle的documentation上一样):

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-> updatedAt 属性。每次创建/更新图像对象时都会调用该方法,因此它将始终保留当前日期。 由于在vich config yaml文件中将inject_on_load参数设置为true,这也可能发生。

我建议您添加一个名为vichUpdatedAt()的新属性,并使用它代替updatedAt():

  • vichUpdatedAt将由捆绑软件使用。
  • updatedAt将用于其他所有功能。

奖励::我建议您安装以下扩展:https://symfony.com/doc/current/bundles/StofDoctrineExtensionsBundle/index.html 它将允许您轻松地在任意位置添加udpatedAt和createdAt属性。

激活所需的扩展名(在/config/packages/stof_doctrine_extensions.yaml 中),因此对于我们来说,这将是具有时间戳记的扩展名:

stof_doctrine_extensions:
    default_locale: en_US
    orm:
        default:
            timestampable: true

然后在您的图像类中,只需添加:

use Gedmo\Timestampable\Traits\TimestampableEntity;
class Image {
   use TimestampableEntity;
   ....
}

然后您就可以使用免费的updatedAt和createdAt属性,无需执行任何其他操作,就可以正常工作。