Blameable

← Blameable

Ashterix

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

en/docs/doctrine/doctrine-behaviors/blameable.md new file +63 āˆ’0

1 + # Blameable
2 +
3 + Stores who created, last changed and deleted an entity.
4 +
5 + ## Entity
6 +
7 + ```php
8 + use Doctrine\ORM\Mapping as ORM;
9 + use Ufo\DoctrineBehaviors\Contract\Entity\BlameableInterface;
10 + use Ufo\DoctrineBehaviors\Model\Blameable\BlameableTrait;
11 +
12 + #[ORM\Entity]
13 + class Category implements BlameableInterface
14 + {
15 + use BlameableTrait;
16 +
17 + #[ORM\Id, ORM\GeneratedValue, ORM\Column]
18 + private ?int $id = null;
19 + }
20 + ```
21 +
22 + ## Mapping
23 +
24 + Depends on the `doctrine_behaviors_blameable_user_entity` parameter:
25 +
26 + | Parameter | Fields `createdBy`, `updatedBy`, `deletedBy` |
27 + | --- | --- |
28 + | not set (`null`) | string columns, nullable |
29 + | user entity class | `ManyToOne` to that entity (columns `createdBy_id` etc., `ON DELETE SET NULL`) |
30 +
31 + ```yaml
32 + # config/services.yaml
33 + parameters:
34 + doctrine_behaviors_blameable_user_entity: App\Entity\User
35 + ```
36 +
37 + A field already mapped in the entity is left alone.
38 +
39 + ## How it works
40 +
41 + The user is returned by `UserProviderInterface` (see [Configuration](/docs/doctrine/doctrine-behaviors/config)). If it is `null` (no token, a console command), values are not changed.
42 +
43 + | Event | What happens |
44 + | --- | --- |
45 + | `prePersist` | `createdBy` and `updatedBy`, if empty |
46 + | `preUpdate` | `updatedBy` |
47 + | `preRemove` | `deletedBy` |
48 +
49 + :::warning
50 + The default `UserProvider` returns the user object from the token. If the `doctrine_behaviors_blameable_user_entity` parameter is not set, the columns are strings — in that case either set the parameter or plug in a provider that returns a string (a `getUserIdentifier()` example is in [Configuration](/docs/doctrine/doctrine-behaviors/config)).
51 + :::
52 +
53 + ## Usage
54 +
55 + ```php
56 + $category = new Category();
57 + $entityManager->persist($category);
58 + $entityManager->flush();
59 +
60 + $category->getCreatedBy(); // App\Entity\User or a string — depending on the mapping
61 + $category->getUpdatedBy();
62 + $category->getDeletedBy(); // null
63 + ```