Reputation: 465
Symfony 4 Entity datetime field returns current date instead of database.
I have an Entity image that stores the date of update in a field called 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;
}
}
But when I retrieve it from the database, the updatedAt field shows the current datatime as 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 ▶}
}
and a var_dump($this->updatedAt)
in the getUpdatedAt method shows this:
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" }
note the 2 different objects. I don't understand why it does so.
Upvotes: 3
Views: 3789
Reputation: 1253
As the comments in your code states (as you kept it from the official VichUploaderBundle's 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();
}
}
So this means that everytime you call your setFile() method, you will assign the current date to your $this->updatedAt property. The Method is called everytime you create/update an Image Object, so it will always hold the current date. This could also happend because of the inject_on_load parameter set to true in the vich config yaml file.
I suggest that you add a new property called vichUpdatedAt() and use it instead of updatedAt() :
BONUS: I encourage you to install these extensions : https://symfony.com/doc/current/bundles/StofDoctrineExtensionsBundle/index.html It will allow you to add udpatedAt & createdAt properties easely anywhere you like.
Activate the extension you want (in /config/packages/stof_doctrine_extensions.yaml), so for us right now that would be the timestampable one :
stof_doctrine_extensions:
default_locale: en_US
orm:
default:
timestampable: true
Then in your image class, just add :
use Gedmo\Timestampable\Traits\TimestampableEntity;
class Image {
use TimestampableEntity;
....
}
And there you go, free updatedAt & createdAt properties, nothing else to do, its just working.
Upvotes: 2