Installing Doctrine Behaviors

← Installing Doctrine Behaviors

Ashterix

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

en/docs/doctrine/doctrine-behaviors/installation.md new file +97 −0

Installing Doctrine Behaviors

Requirements: PHP 8.4+, Symfony ^7.0 or ^8.0, doctrine/orm ^3.7, doctrine/dbal ^4.2, doctrine/persistence ^4.0, doctrine/doctrine-bundle ^3.0, symfony/security-bundle ^7.0 | ^8.0. For PHP 8.2–8.3, DBAL 3 or DoctrineBundle 2 there is 1.0.4, but it has bugs fixed only in 2.0.0 (see Fixed issues).

Step 0: Symfony Flex recipes

For Flex to register the bundle automatically, add the UFO-Tech recipe repository to your project's composer.json:

{
    "extra": {
        "symfony": {
            "endpoint": [
                "https://api.github.com/repos/ufo-tech/recipes/contents/index.json?ref=main",
                "flex://defaults"
            ]
        }
    }
}

Step 1: Installation

composer require ufo-tech/doctrine-behaviors

The recipe only adds the bundle to config/bundles.php; it creates no configuration files.

Step 2: Registering the bundle

Without the recipe, add it to config/bundles.php manually:

return [
    // ...
    Ufo\DoctrineBehaviors\DoctrineBehaviorsBundle::class => ['all' => true],
];

The bundle has no configuration section of its own (doctrine_behaviors:): defaults are changed with container parameters — see Configuration.

Step 3: The first behavior

Add the interface and the trait to an 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;
}

Step 4: Database schema

The bundle changes the entity mapping, so update the schema — with a migration:

bin/console doctrine:migrations:diff
bin/console doctrine:migrations:migrate

or directly (for development):

bin/console doctrine:schema:update --force

The SQL shows which columns the bundle adds: bin/console doctrine:schema:update --dump-sql.

The bundle's listeners are registered only for the default Doctrine connection. Entities managed by another connection's manager get no behaviors — see Known issues.

PHPStan

The package ships a PHPStan extension that refines the types of translate(), getTranslations() and getTranslatable(). With phpstan/extension-installer it is included automatically; without it, add to phpstan.neon:

includes:
    - vendor/ufo-tech/doctrine-behaviors/phpstan-extension.neon

What's next?