Skip to content

Commit

Permalink
refactor(entity): add CreatableUpdateableTrait
Browse files Browse the repository at this point in the history
  • Loading branch information
n3wborn committed Jan 13, 2024
1 parent 49aeda0 commit e3b5f49
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/Entity/CreatableUpdateableTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

trait CreatableUpdateableTrait
{
#[ORM\Column(type: 'datetime_immutable')]
private ?\DateTimeImmutable $createdAt = null;

#[ORM\Column(nullable: true, type: 'datetime_immutable')]
private ?\DateTimeImmutable $updatedAt = null;

final public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->createdAt;
}

final public function setCreatedAt(\DateTimeImmutable $createdAt): self
{
$this->createdAt = $createdAt;

return $this;
}

final public function getUpdatedAt(): ?\DateTimeImmutable
{
return $this->updatedAt;
}

final public function setUpdatedAt(?\DateTimeImmutable $updatedAt): self
{
$this->updatedAt = $updatedAt;

return $this;
}

#[ORM\PrePersist]
final public function setCreatedAtValue(): void
{
$this->createdAt = new \DateTimeImmutable();
}

#[ORM\PreUpdate]
final public function setUpdatedAtValue(): void
{
$this->updatedAt = new \DateTimeImmutable();
}
}

0 comments on commit e3b5f49

Please sign in to comment.