Doctrine Behaviors

← Doctrine Behaviors

Ashterix

docs: Doctrine section with Doctrine Behaviors and Doctrine Helper (uk + en)

en/docs/doctrine/doctrine-behaviors/README.md new file +60 −0

Doctrine Behaviors

<include>(docs/doctrine/doctrine-behaviors/.info.md)

A set of behaviors for Doctrine ORM entities in Symfony 7 / 8: timestamps, change authors, soft delete, translations, slugs, trees, UUIDs and a change log. A behavior is enabled with an interface and a trait on the entity — the bundle adds the required columns to the mapping and fills them in Doctrine events.

The package is a fork of KnpLabs/DoctrineBehaviors (via the intermediate fork Miets/DoctrineBehaviors). The original does not work with Symfony newer than 6.3 or with DBAL 4; the UFO-Tech fork updates the code for Symfony 7+ / 8, Doctrine ORM 3 and DBAL 4. The ideas, the API and most of the code come from the KnpLabs authors, MIT license.

This documentation describes version 2.0.0.

What it looks like

use Doctrine\ORM\Mapping as ORM;
use Ufo\DoctrineBehaviors\Contract\Entity\SoftDeletableInterface;
use Ufo\DoctrineBehaviors\Contract\Entity\TimestampableInterface;
use Ufo\DoctrineBehaviors\Model\SoftDeletable\SoftDeletableTrait;
use Ufo\DoctrineBehaviors\Model\Timestampable\TimestampableTrait;

#[ORM\Entity]
class Article implements TimestampableInterface, SoftDeletableInterface
{
    use TimestampableTrait;
    use SoftDeletableTrait;

    #[ORM\Id, ORM\GeneratedValue, ORM\Column]
    private ?int $id = null;
}

The createdAt, updatedAt and deletedAt columns appear in the mapping without any #[ORM\Column]; timestamps are filled in during flush(), and $entityManager->remove($article) only sets deletedAt.

Behaviors

Behavior Interface Trait What it adds
Timestampable TimestampableInterface TimestampableTrait createdAt, updatedAt
Blameable BlameableInterface BlameableTrait createdBy, updatedBy, deletedBy
SoftDeletable SoftDeletableInterface SoftDeletableTrait deletedAt, deletion without DELETE
Translatable TranslatableInterface, TranslationInterface TranslatableTrait, TranslationTrait relation to the translation entity, locale
Sluggable SluggableInterface SluggableTrait slug
Tree TreeNodeInterface TreeNodeTrait, TreeTrait (repository) materializedPath
Uuidable UuidableInterface UuidableTrait uuid
Loggable LoggableInterface LoggableTrait entries in a PSR-3 logger

Interfaces live in Ufo\DoctrineBehaviors\Contract\Entity, traits in Ufo\DoctrineBehaviors\Model\<Behavior>. Every trait except LoggableTrait is made of two: ...PropertiesTrait (properties) and ...MethodsTrait (methods) — they can be used separately when you need to declare the properties your own way.

The bundle adds a column only if a field with that name is not mapped yet, so you can set the type or length with your own mapping.

Sections

en/docs/doctrine/doctrine-behaviors/.info.md new file +19 −0

Version Downloads Stars

composer require ufo-tech/doctrine-behaviors
  • PHP 8.4+
  • Symfony 7 · 8
  • Built on Doctrine ORM 3.7+ · DBAL 4.2+
  • Type Symfony Bundle
  • Category Doctrine
  • License MIT