feat: add createdAt and updatedAt on Domain entity

This commit is contained in:
Maël Gangloff
2024-07-17 19:29:43 +02:00
parent 358cad6c21
commit 11c7207a1c
3 changed files with 77 additions and 1 deletions

View File

@@ -8,6 +8,7 @@ use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Post;
use App\Controller\DomainRefreshController;
use App\Repository\DomainRepository;
use DateTimeImmutable;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
@@ -98,6 +99,12 @@ class Domain
#[Groups(['domain:item'])]
private Collection $nameservers;
#[ORM\Column(type: Types::DATE_IMMUTABLE)]
private ?DateTimeImmutable $createdAt = null;
#[ORM\Column(type: Types::DATE_IMMUTABLE)]
private ?DateTimeImmutable $updatedAt = null;
public function __construct()
{
$this->events = new ArrayCollection();
@@ -252,4 +259,36 @@ class Domain
return $this;
}
public function getUpdatedAt(): ?DateTimeImmutable
{
return $this->updatedAt;
}
#[ORM\PrePersist]
#[ORM\PreUpdate]
public function updateTimestamps(): void
{
$this->setUpdatedAt(new DateTimeImmutable('now'));
if ($this->getCreatedAt() === null) {
$this->setCreatedAt(new DateTimeImmutable('now'));
}
}
private function setUpdatedAt(?DateTimeImmutable $updatedAt): void
{
$this->updatedAt = $updatedAt;
}
public function getCreatedAt(): ?DateTimeImmutable
{
return $this->createdAt;
}
private function setCreatedAt(?DateTimeImmutable $createdAt): void
{
$this->createdAt = $createdAt;
}
}