Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Entity\Traits;
- use DateTime;
- use DateTimeInterface;
- use Doctrine\DBAL\Types\Types;
- use Exception;
- use Doctrine\ORM\Mapping as ORM;
- trait TimestampTrait
- {
- #[ORM\Column(type: Types::DATETIME_IMMUTABLE, nullable: true)]
- private ?DateTime $createdAt = null;
- #[ORM\Column(type: Types::DATETIME_IMMUTABLE, nullable: true)]
- private ?DateTime $updatedAt = null;
- /**
- * @return DateTimeInterface|null
- * @throws Exception
- */
- public function getCreatedAt(): ?DateTimeInterface
- {
- return $this->createdAt ?? new DateTime();
- }
- /**
- * @param DateTimeInterface $createdAt
- * @return $this
- */
- public function setCreatedAt(DateTimeInterface $createdAt): self
- {
- $this->createdAt = $createdAt;
- return $this;
- }
- /**
- * @return DateTimeInterface|null
- */
- public function getUpdatedAt(): ?DateTimeInterface
- {
- return $this->updatedAt ?? new DateTime();
- }
- /**
- * @param DateTimeInterface $updatedAt
- * @return $this
- */
- public function setUpdatedAt(DateTimeInterface $updatedAt): self
- {
- $this->updatedAt = $updatedAt;
- return $this;
- }
- #[ORM\PrePersist]
- #[ORM\PreUpdate]
- public function updateTimestamps(): void
- {
- $now = new DateTime();
- $this->setUpdatedAt($now);
- if ($this->getId() === null) {
- $this->setCreatedAt($now);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement