| @@ 8-39 (lines=32) @@ | ||
| 5 | use Doctrine\ORM\Mapping\Column; |
|
| 6 | use Doctrine\ORM\Mapping\PrePersist; |
|
| 7 | ||
| 8 | trait CreatedAt |
|
| 9 | { |
|
| 10 | /** |
|
| 11 | * @Column(type="datetime", name="created_at") |
|
| 12 | */ |
|
| 13 | private $created_at; |
|
| 14 | ||
| 15 | /** |
|
| 16 | * Getter |
|
| 17 | */ |
|
| 18 | public function getCreatedAt($format = null) |
|
| 19 | { |
|
| 20 | switch (true) { |
|
| 21 | case is_string($this->created_at): |
|
| 22 | case is_object($this->created_at) && get_class($this->created_at) !== 'DateTime': |
|
| 23 | throw new \Exception("created_at is not a datetime", 400); |
|
| 24 | case $this->created_at === null: |
|
| 25 | case $format === null: |
|
| 26 | return $this->created_at; |
|
| 27 | default: |
|
| 28 | return $this->created_at->format($format); |
|
| 29 | } |
|
| 30 | } |
|
| 31 | ||
| 32 | /** |
|
| 33 | * @PrePersist |
|
| 34 | */ |
|
| 35 | public function setCreatedAtBeforePersist() |
|
| 36 | { |
|
| 37 | $this->created_at = new \DateTime(date('Y-m-d H:i:s')); |
|
| 38 | } |
|
| 39 | } |
|
| 40 | ||
| @@ 9-42 (lines=34) @@ | ||
| 6 | use Doctrine\ORM\Mapping\PrePersist; |
|
| 7 | use Doctrine\ORM\Mapping\PreUpdate; |
|
| 8 | ||
| 9 | trait UpdatedAt |
|
| 10 | { |
|
| 11 | /** |
|
| 12 | * @Column(type="datetime", name="updated_at") |
|
| 13 | */ |
|
| 14 | private $updated_at; |
|
| 15 | ||
| 16 | /** |
|
| 17 | * Getter |
|
| 18 | */ |
|
| 19 | public function getUpdatedAt($format = null) |
|
| 20 | { |
|
| 21 | switch (true) { |
|
| 22 | case is_string($this->updated_at): |
|
| 23 | case is_object($this->updated_at) && get_class($this->updated_at) !== 'DateTime': |
|
| 24 | throw new \Exception("updated_at is not a datetime", 400); |
|
| 25 | case $this->updated_at === null: |
|
| 26 | case $format === null: |
|
| 27 | return $this->updated_at; |
|
| 28 | default: |
|
| 29 | return $this->updated_at->format($format); |
|
| 30 | } |
|
| 31 | } |
|
| 32 | ||
| 33 | /** |
|
| 34 | * @PreUpdate |
|
| 35 | * @PrePersist |
|
| 36 | */ |
|
| 37 | public function setUpdatedAt() |
|
| 38 | { |
|
| 39 | $this->updated_at = new \DateTime(date('Y-m-d H:i:s')); |
|
| 40 | } |
|
| 41 | } |
|
| 42 | ||