SoftDeletable

← SoftDeletable

Ashterix

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

en/docs/doctrine/doctrine-behaviors/soft_deletable.md new file +65 āˆ’0

1 + # SoftDeletable
2 +
3 + Soft delete: `remove()` does not delete the row but stores the deletion time in `deletedAt`.
4 +
5 + ## Entity
6 +
7 + ```php
8 + use Doctrine\ORM\Mapping as ORM;
9 + use Ufo\DoctrineBehaviors\Contract\Entity\SoftDeletableInterface;
10 + use Ufo\DoctrineBehaviors\Model\SoftDeletable\SoftDeletableTrait;
11 +
12 + #[ORM\Entity]
13 + class Category implements SoftDeletableInterface
14 + {
15 + use SoftDeletableTrait;
16 +
17 + #[ORM\Id, ORM\GeneratedValue, ORM\Column]
18 + private ?int $id = null;
19 + }
20 + ```
21 +
22 + The bundle maps `deletedAt` (`datetime`, nullable).
23 +
24 + ## How it works
25 +
26 + In `onFlush` every `SoftDeletableInterface` entity scheduled for deletion gets `delete()` (the current time in `deletedAt`) and is persisted again — an `UPDATE` runs instead of `DELETE`.
27 +
28 + :::warning
29 + There is no query filter: `find()`, repositories and DQL return "deleted" entities too. Filter them out yourself — with a `deletedAt IS NULL` condition or an `isDeleted()` check.
30 + :::
31 +
32 + Doctrine performs cascade removal of related entities as usual: only those that also implement `SoftDeletableInterface` are soft-deleted.
33 +
34 + ## Usage
35 +
36 + ```php
37 + $entityManager->remove($category);
38 + $entityManager->flush();
39 +
40 + $category = $categoryRepository->find($id); // the entity is still there
41 + $category->isDeleted(); // true
42 + ```
43 +
44 + ### Restore
45 +
46 + ```php
47 + $category->restore(); // deletedAt = null
48 + $category->isDeleted(); // false
49 + $entityManager->flush();
50 + ```
51 +
52 + ### Delete at a given time
53 +
54 + ```php
55 + $category->setDeletedAt(new \DateTime('+1 day'));
56 + $category->isDeleted(); // false — until the date comes
57 + $category->willBeDeleted(); // true
58 + $entityManager->flush();
59 + ```
60 +
61 + | Method | Returns |
62 + | --- | --- |
63 + | `isDeleted()` | `true` if `deletedAt` is set and has already come |
64 + | `willBeDeleted(?DateTimeInterface $at = null)` | `true` if `deletedAt` is set (and, when `$at` is passed, not later than it) |
65 + | `getDeletedAt()` | `?DateTimeInterface` |