Skip to content

Doctrine

Timestampable

App\Core\Doctrine\Timestampable is a trait usuble in an entity. It adds createdAt and updatedAt datetime attributes with the setters and the getters :

  • setCreatedAt(?\DateTime $createdAt): self
  • setUpdated(?\DateTime $createdAt): self
  • getCreatedAt(): ?\DateTime
  • getUpdatedAt(): ?\DateTime

When the entity is created or updated, createdAt and updatedAt are automatically updated to.

Usage

src/Entity/FooEntity.php
namespace App/Entity;

use use App\Core\Entity\EntityInterface;
use Doctrine\ORM\Mapping as ORM;

use App\Core\Doctrine\Timestampable;
use App\Core\Entity\EntityInterface;
use App\Repository\FooRepository;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: FooRepository::class)]
#[ORM\HasLifecycleCallbacks]
class FooEntity implements EntityInterface
{
    use Timestampable;

    // ...
}