-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from aoirontec/trait-timestampable
Add TimestampableTrait
- Loading branch information
Showing
3 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# TimestampableTrait | ||
|
||
Trait para gestionar la fecha UTC de creación/actualización de una entidad. | ||
|
||
````php | ||
<?php | ||
|
||
namespace App\Entity; | ||
|
||
use \Irontec\DoctrineEntityTrait\TimestampableTrait; | ||
|
||
class MyEntity | ||
{ | ||
... | ||
use TimestampableTrait; | ||
... | ||
} | ||
```` | ||
|
||
# Fields | ||
|
||
+ createdAt | ||
+ updatedAt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the DoctrineEntityTrait. | ||
*/ | ||
|
||
namespace Irontec\DoctrineEntityTrait; | ||
|
||
use DateTimeImmutable; | ||
use DateTimeZone; | ||
use Doctrine\DBAL\Types\Types; | ||
use Doctrine\ORM\Mapping as ORM; | ||
use Symfony\Component\Serializer\Annotation\Groups; | ||
|
||
/** | ||
* Entities using this Trait must have the | ||
* `#[ORM\HasLifecycleCallbacks]` attribute. | ||
*/ | ||
trait TimestampableTrait | ||
{ | ||
#[ORM\Column(type: Types::DATETIME_IMMUTABLE, options: ['default' => 'CURRENT_TIMESTAMP'])] | ||
#[Groups(['timestampable'])] | ||
protected ?DateTimeImmutable $createdAt = null; | ||
|
||
#[ORM\Column(type: Types::DATETIME_IMMUTABLE, options: ['default' => 'CURRENT_TIMESTAMP'])] | ||
#[Groups(['timestampable'])] | ||
protected ?DateTimeImmutable $updatedAt = null; | ||
|
||
public function getCreatedAt(): ?DateTimeImmutable | ||
{ | ||
return $this->createdAt; | ||
} | ||
|
||
public function getUpdatedAt(): ?DateTimeImmutable | ||
{ | ||
return $this->updatedAt; | ||
} | ||
|
||
#[ORM\PrePersist] | ||
public function createTimestamps(): void | ||
{ | ||
$now = new DateTimeImmutable('now', timezone: new DateTimeZone('UTC')); | ||
$this->createdAt = $now; | ||
$this->updatedAt = $now; | ||
} | ||
|
||
#[ORM\PreUpdate] | ||
public function updateTimestamps(): void | ||
{ | ||
$this->updatedAt = new DateTimeImmutable('now', timezone: new DateTimeZone('UTC')); | ||
} | ||
} |