-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
…gning AbstractEntity as MappedSuperclass for doctrine
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Api\App\Entity; | ||
|
||
use DateTimeImmutable; | ||
use Doctrine\ORM\Mapping as ORM; | ||
Check warning on line 8 in src/App/src/Entity/TimestampsTrait.php
|
||
|
||
#[ORM\HasLifecycleCallbacks] | ||
Check warning on line 10 in src/App/src/Entity/TimestampsTrait.php
|
||
trait TimestampsTrait | ||
{ | ||
#[ORM\Column(name: "created", type: "datetime_immutable")] | ||
Check warning on line 13 in src/App/src/Entity/TimestampsTrait.php
|
||
protected DateTimeImmutable $created; | ||
|
||
#[ORM\Column(name: "updated", type: "datetime_immutable", nullable: true)] | ||
Check warning on line 16 in src/App/src/Entity/TimestampsTrait.php
|
||
protected ?DateTimeImmutable $updated = null; | ||
|
||
public function getCreated(): ?DateTimeImmutable | ||
{ | ||
return $this->created; | ||
} | ||
|
||
public function getCreatedFormatted(string $dateFormat = 'Y-m-d H:i:s'): string | ||
{ | ||
return $this->created->format($dateFormat); | ||
} | ||
|
||
public function getUpdated(): ?DateTimeImmutable | ||
{ | ||
return $this->updated; | ||
} | ||
|
||
public function getUpdatedFormatted(string $dateFormat = 'Y-m-d H:i:s'): ?string | ||
{ | ||
if ($this->updated instanceof DateTimeImmutable) { | ||
return $this->updated->format($dateFormat); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
#[ORM\PrePersist] | ||
Check warning on line 43 in src/App/src/Entity/TimestampsTrait.php
|
||
public function created(): void | ||
{ | ||
$this->created = new DateTimeImmutable(); | ||
$this->updated = new DateTimeImmutable(); | ||
} | ||
|
||
#[ORM\PreUpdate] | ||
Check warning on line 50 in src/App/src/Entity/TimestampsTrait.php
|
||
public function touch(): void | ||
{ | ||
$this->updated = new DateTimeImmutable(); | ||
} | ||
} |