src/Entity/AbstractBaseEntity.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use \DateTimeInterface;
  5. /**
  6.  * @ORM\MappedSuperclass()
  7.  */
  8. abstract class AbstractBaseEntity
  9. {
  10.     /**
  11.      * @ORM\Id
  12.      * @ORM\GeneratedValue
  13.      * @ORM\Column(type="integer")
  14.      */
  15.     protected $id;
  16.     /**
  17.      * @ORM\Column(type="datetime")
  18.      */
  19.     protected DateTimeInterface $created;
  20.     /**
  21.      * @ORM\Column(type="datetime")
  22.      */
  23.     protected DateTimeInterface $updated;
  24.     /**
  25.      * @return int|null
  26.      */
  27.     public function getId(): ?int
  28.     {
  29.         return $this->id;
  30.     }
  31.     /**
  32.      * @return DateTimeInterface|null
  33.      */
  34.     public function getCreated(): ?DateTimeInterface
  35.     {
  36.         return $this->created;
  37.     }
  38.     /**
  39.      * @param DateTimeInterface $created
  40.      * @return $this
  41.      */
  42.     public function setCreated(DateTimeInterface $created): self
  43.     {
  44.         $this->created $created;
  45.         return $this;
  46.     }
  47.     /**
  48.      * @return DateTimeInterface|null
  49.      */
  50.     public function getUpdated(): ?DateTimeInterface
  51.     {
  52.         return $this->updated;
  53.     }
  54.     /**
  55.      * @param DateTimeInterface $updated
  56.      * @return $this
  57.      */
  58.     public function setUpdated(DateTimeInterface $updated): self
  59.     {
  60.         $this->updated $updated;
  61.         return $this;
  62.     }
  63.     /**
  64.      * constructor
  65.      */
  66.     public function __construct()
  67.     {
  68.         $currentTimestamp = new \DateTime();
  69.         $this->created $currentTimestamp;
  70.         $this->updated $currentTimestamp;
  71.     }
  72.     /**
  73.      * @ORM\PreUpdate()
  74.      * @return void
  75.      */
  76.     public function updateTimestamp(): void
  77.     {
  78.         $currentTimestamp = new \DateTime();
  79.         $this->updated $currentTimestamp;
  80.         if (is_null($this->getCreated())) {
  81.             $this->setCreated($currentTimestamp);
  82.         }
  83.     }
  84. }