diff --git a/composer.lock b/composer.lock
index 1db25d3fe..908769353 100644
--- a/composer.lock
+++ b/composer.lock
@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
- "content-hash": "00722ce3778eecbdefc65a10ebf0bba7",
+ "content-hash": "a0eeb54dbd1af337628325e833d9d409",
"packages": [
{
"name": "dflydev/dot-access-data",
diff --git a/src/Entity/Comment.php b/src/Entity/Comment.php
index aa1b8e6bc..a69e6cc09 100644
--- a/src/Entity/Comment.php
+++ b/src/Entity/Comment.php
@@ -13,6 +13,9 @@
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
+use Doctrine\ORM\Mapping\HasLifecycleCallbacks;
+use Doctrine\ORM\Mapping\PrePersist;
+use Doctrine\ORM\Mapping\PreUpdate;
use Symfony\Component\Validator\Constraints as Assert;
use function Symfony\Component\String\u;
@@ -25,9 +28,11 @@
*
* @author Ryan Weaver
* @author Javier Eguiluz
+ * @author Mecanik
*/
#[ORM\Entity]
#[ORM\Table(name: 'symfony_demo_comment')]
+#[HasLifecycleCallbacks]
class Comment
{
#[ORM\Id]
@@ -51,6 +56,12 @@ class Comment
#[ORM\JoinColumn(nullable: false)]
private ?User $author = null;
+ #[ORM\Column(type: Types::DATETIMET_IMMUTABLE)]
+ private $createdAt;
+
+ #[ORM\Column(type: Types::DATETIMET_IMMUTABLE)]
+ private $updatedAt;
+
public function __construct()
{
$this->publishedAt = new \DateTime();
@@ -108,4 +119,46 @@ public function setPost(Post $post): void
{
$this->post = $post;
}
+
+ public function getCreatedAt(): ?\DateTimeImmutable
+ {
+ return $this->createdAt;
+ }
+
+ public function setCreatedAt(\DateTimeImmutable $createdAt): self
+ {
+ $this->createdAt = $createdAt;
+
+ return $this;
+ }
+
+ public function getUpdatedAt(): ?\DateTimeImmutable
+ {
+ return $this->updatedAt;
+ }
+
+ public function setUpdatedAt(\DateTimeImmutable $updatedAt): self
+ {
+ $this->updatedAt = $updatedAt;
+
+ return $this;
+ }
+
+ #[PrePersist]
+ public function onPrePersist()
+ {
+ if (null === $this->getCreatedAt()) {
+ $this->setCreatedAt(new \DateTimeImmutable('now'));
+ }
+
+ if ($this->getUpdatedAt() == null) {
+ $this->setUpdatedAt(new \DateTimeImmutable('now'));
+ }
+ }
+
+ #[PreUpdate]
+ public function onPreUpdate()
+ {
+ $this->setUpdatedAt(new \DateTimeImmutable('now'));
+ }
}
diff --git a/src/Entity/Post.php b/src/Entity/Post.php
index 8d1913697..32912cbb8 100644
--- a/src/Entity/Post.php
+++ b/src/Entity/Post.php
@@ -16,6 +16,9 @@
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
+use Doctrine\ORM\Mapping\HasLifecycleCallbacks;
+use Doctrine\ORM\Mapping\PrePersist;
+use Doctrine\ORM\Mapping\PreUpdate;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
@@ -30,10 +33,12 @@
* @author Ryan Weaver
* @author Javier Eguiluz
* @author Yonel Ceruto
+ * @author Mecanik
*/
#[ORM\Entity(repositoryClass: PostRepository::class)]
#[ORM\Table(name: 'symfony_demo_post')]
#[UniqueEntity(fields: ['slug'], errorPath: 'title', message: 'post.slug_unique')]
+#[HasLifecycleCallbacks]
class Post
{
#[ORM\Id]
@@ -81,6 +86,12 @@ class Post
#[Assert\Count(max: 4, maxMessage: 'post.too_many_tags')]
private Collection $tags;
+ #[ORM\Column(type: Types::DATETIMET_IMMUTABLE)]
+ private $createdAt;
+
+ #[ORM\Column(type: Types::DATETIMET_IMMUTABLE)]
+ private $updatedAt;
+
public function __construct()
{
$this->publishedAt = new \DateTime();
@@ -195,4 +206,46 @@ public function getTags(): Collection
{
return $this->tags;
}
+
+ public function getCreatedAt(): ?\DateTimeImmutable
+ {
+ return $this->createdAt;
+ }
+
+ public function setCreatedAt(\DateTimeImmutable $createdAt): self
+ {
+ $this->createdAt = $createdAt;
+
+ return $this;
+ }
+
+ public function getUpdatedAt(): ?\DateTimeImmutable
+ {
+ return $this->updatedAt;
+ }
+
+ public function setUpdatedAt(\DateTimeImmutable $updatedAt): self
+ {
+ $this->updatedAt = $updatedAt;
+
+ return $this;
+ }
+
+ #[PrePersist]
+ public function onPrePersist()
+ {
+ if (null === $this->getCreatedAt()) {
+ $this->setCreatedAt(new \DateTimeImmutable('now'));
+ }
+
+ if ($this->getUpdatedAt() == null) {
+ $this->setUpdatedAt(new \DateTimeImmutable('now'));
+ }
+ }
+
+ #[PreUpdate]
+ public function onPreUpdate()
+ {
+ $this->setUpdatedAt(new \DateTimeImmutable('now'));
+ }
}
diff --git a/src/Entity/Tag.php b/src/Entity/Tag.php
index 0979290f4..0b2918f19 100644
--- a/src/Entity/Tag.php
+++ b/src/Entity/Tag.php
@@ -13,6 +13,9 @@
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
+use Doctrine\ORM\Mapping\HasLifecycleCallbacks;
+use Doctrine\ORM\Mapping\PrePersist;
+use Doctrine\ORM\Mapping\PreUpdate;
/**
* Defines the properties of the Tag entity to represent the post tags.
@@ -20,9 +23,11 @@
* See https://symfony.com/doc/current/doctrine.html#creating-an-entity-class
*
* @author Yonel Ceruto
+ * @author Mecanik
*/
#[ORM\Entity]
#[ORM\Table(name: 'symfony_demo_tag')]
+#[HasLifecycleCallbacks]
class Tag implements \JsonSerializable
{
#[ORM\Id]
@@ -33,6 +38,12 @@ class Tag implements \JsonSerializable
#[ORM\Column(type: Types::STRING, unique: true)]
private readonly string $name;
+ #[ORM\Column(type: Types::DATETIMET_IMMUTABLE)]
+ private $createdAt;
+
+ #[ORM\Column(type: Types::DATETIMET_IMMUTABLE)]
+ private $updatedAt;
+
public function __construct(string $name)
{
$this->name = $name;
@@ -61,4 +72,46 @@ public function __toString(): string
{
return $this->name;
}
+
+ public function getCreatedAt(): ?\DateTimeImmutable
+ {
+ return $this->createdAt;
+ }
+
+ public function setCreatedAt(\DateTimeImmutable $createdAt): self
+ {
+ $this->createdAt = $createdAt;
+
+ return $this;
+ }
+
+ public function getUpdatedAt(): ?\DateTimeImmutable
+ {
+ return $this->updatedAt;
+ }
+
+ public function setUpdatedAt(\DateTimeImmutable $updatedAt): self
+ {
+ $this->updatedAt = $updatedAt;
+
+ return $this;
+ }
+
+ #[PrePersist]
+ public function onPrePersist()
+ {
+ if (null === $this->getCreatedAt()) {
+ $this->setCreatedAt(new \DateTimeImmutable('now'));
+ }
+
+ if ($this->getUpdatedAt() == null) {
+ $this->setUpdatedAt(new \DateTimeImmutable('now'));
+ }
+ }
+
+ #[PreUpdate]
+ public function onPreUpdate()
+ {
+ $this->setUpdatedAt(new \DateTimeImmutable('now'));
+ }
}
diff --git a/src/Entity/User.php b/src/Entity/User.php
index 547ee659f..a471af3ac 100644
--- a/src/Entity/User.php
+++ b/src/Entity/User.php
@@ -14,6 +14,9 @@
use App\Repository\UserRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
+use Doctrine\ORM\Mapping\HasLifecycleCallbacks;
+use Doctrine\ORM\Mapping\PrePersist;
+use Doctrine\ORM\Mapping\PreUpdate;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;
@@ -27,9 +30,11 @@
*
* @author Ryan Weaver
* @author Javier Eguiluz
+ * @author Mecanik
*/
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[ORM\Table(name: 'symfony_demo_user')]
+#[HasLifecycleCallbacks]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
// We can use constants for roles to find usages all over the application rather
@@ -59,6 +64,12 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
#[ORM\Column(type: Types::STRING)]
private ?string $password = null;
+ #[ORM\Column(type: Types::DATETIMET_IMMUTABLE)]
+ private $createdAt;
+
+ #[ORM\Column(type: Types::DATETIMET_IMMUTABLE)]
+ private $updatedAt;
+
/**
* @var string[]
*/
@@ -180,4 +191,46 @@ public function __unserialize(array $data): void
// add $this->salt too if you don't use Bcrypt or Argon2i
[$this->id, $this->username, $this->password] = $data;
}
+
+ public function getCreatedAt(): ?\DateTimeImmutable
+ {
+ return $this->createdAt;
+ }
+
+ public function setCreatedAt(\DateTimeImmutable $createdAt): self
+ {
+ $this->createdAt = $createdAt;
+
+ return $this;
+ }
+
+ public function getUpdatedAt(): ?\DateTimeImmutable
+ {
+ return $this->updatedAt;
+ }
+
+ public function setUpdatedAt(\DateTimeImmutable $updatedAt): self
+ {
+ $this->updatedAt = $updatedAt;
+
+ return $this;
+ }
+
+ #[PrePersist]
+ public function onPrePersist()
+ {
+ if (null === $this->getCreatedAt()) {
+ $this->setCreatedAt(new \DateTimeImmutable('now'));
+ }
+
+ if ($this->getUpdatedAt() == null) {
+ $this->setUpdatedAt(new \DateTimeImmutable('now'));
+ }
+ }
+
+ #[PreUpdate]
+ public function onPreUpdate()
+ {
+ $this->setUpdatedAt(new \DateTimeImmutable('now'));
+ }
}