docs: Doctrine section with Doctrine Behaviors and Doctrine Helper (uk + en)
en/docs/doctrine/doctrine-behaviors/timestampable.md
new file +52
−0
Timestampable
Stores when an entity was created and last changed.
Entity
use Doctrine\ORM\Mapping as ORM;
use Ufo\DoctrineBehaviors\Contract\Entity\TimestampableInterface;
use Ufo\DoctrineBehaviors\Model\Timestampable\TimestampableTrait;
#[ORM\Entity]
class Category implements TimestampableInterface
{
use TimestampableTrait;
#[ORM\Id, ORM\GeneratedValue, ORM\Column]
private ?int $id = null;
}
How it works
- On
loadClassMetadatathe bundle mapscreatedAtandupdatedAt(nullable, type from thedoctrine_behaviors_timestampable_date_field_typeparameter,datetimeby default) and adds theupdateTimestamps()lifecycle callback onprePersistandpreUpdate. The#[ORM\HasLifecycleCallbacks]attribute is not needed. updateTimestamps()setscreatedAtif it is stillnulland always updatesupdatedAt. The time has microseconds and uses thedate_default_timezone_get()time zone.- Doctrine calls
preUpdateonly for entities with changed fields — aflush()without changes leavesupdatedAtalone. #[ORM\MappedSuperclass]is skipped; the columns go to the child entities.
Usage
$category = new Category();
$entityManager->persist($category);
$entityManager->flush();
$category->getCreatedAt(); // DateTime
$category->getUpdatedAt(); // DateTime
setCreatedAt() and setUpdatedAt() let you set values manually; a createdAt set this way is not overwritten on save.
Configuration
# config/services.yaml
parameters:
doctrine_behaviors_timestampable_date_field_type: datetimetz
The trait writes DateTime, so only mutable types fit — datetime and datetimetz. The *_immutable types expect DateTimeImmutable and are not compatible.