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

1 + # Installing Doctrine Behaviors
2 +
3 + :::info
4 + 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](/docs/doctrine/doctrine-behaviors/known_issues/fixed)).
5 + :::
6 +
7 + ## Step 0: Symfony Flex recipes
8 +
9 + For Flex to register the bundle automatically, add the UFO-Tech recipe repository to your project's `composer.json`:
10 +
11 + ```json
12 + {
13 + "extra": {
14 + "symfony": {
15 + "endpoint": [
16 + "https://api.github.com/repos/ufo-tech/recipes/contents/index.json?ref=main",
17 + "flex://defaults"
18 + ]
19 + }
20 + }
21 + }
22 + ```
23 +
24 + ## Step 1: Installation
25 +
26 + ```bash
27 + composer require ufo-tech/doctrine-behaviors
28 + ```
29 +
30 + The recipe only adds the bundle to `config/bundles.php`; it creates no configuration files.
31 +
32 + ## Step 2: Registering the bundle
33 +
34 + Without the recipe, add it to `config/bundles.php` manually:
35 +
36 + ```php
37 + return [
38 + // ...
39 + Ufo\DoctrineBehaviors\DoctrineBehaviorsBundle::class => ['all' => true],
40 + ];
41 + ```
42 +
43 + The bundle has no configuration section of its own (`doctrine_behaviors:`): defaults are changed with container parameters — see [Configuration](/docs/doctrine/doctrine-behaviors/config).
44 +
45 + ## Step 3: The first behavior
46 +
47 + Add the interface and the trait to an entity:
48 +
49 + ```php
50 + use Doctrine\ORM\Mapping as ORM;
51 + use Ufo\DoctrineBehaviors\Contract\Entity\TimestampableInterface;
52 + use Ufo\DoctrineBehaviors\Model\Timestampable\TimestampableTrait;
53 +
54 + #[ORM\Entity]
55 + class Category implements TimestampableInterface
56 + {
57 + use TimestampableTrait;
58 +
59 + #[ORM\Id, ORM\GeneratedValue, ORM\Column]
60 + private ?int $id = null;
61 + }
62 + ```
63 +
64 + ## Step 4: Database schema
65 +
66 + The bundle changes the entity mapping, so update the schema — with a migration:
67 +
68 + ```bash
69 + bin/console doctrine:migrations:diff
70 + bin/console doctrine:migrations:migrate
71 + ```
72 +
73 + or directly (for development):
74 +
75 + ```bash
76 + bin/console doctrine:schema:update --force
77 + ```
78 +
79 + The SQL shows which columns the bundle adds: `bin/console doctrine:schema:update --dump-sql`.
80 +
81 + :::warning
82 + 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](/docs/doctrine/doctrine-behaviors/known_issues/open).
83 + :::
84 +
85 + ## PHPStan
86 +
87 + 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`:
88 +
89 + ```yaml
90 + includes:
91 + - vendor/ufo-tech/doctrine-behaviors/phpstan-extension.neon
92 + ```
93 +
94 + ## What's next?
95 +
96 + * [Configuration](/docs/doctrine/doctrine-behaviors/config) — parameters and providers.
97 + * [Timestampable](/docs/doctrine/doctrine-behaviors/timestampable), [Blameable](/docs/doctrine/doctrine-behaviors/blameable), [SoftDeletable](/docs/doctrine/doctrine-behaviors/soft_deletable), [Translatable](/docs/doctrine/doctrine-behaviors/translatable) — the most used behaviors.