@@ -39,7 +39,6 @@ use Symfony\Bridge\Doctrine\Types\UuidType;
3939use Symfony\Component\Uid\Uuid;
4040
4141#[ORM\Entity()]
42- #[ORM\Index(columns: ['path'])]
4342#[ORM\EntityListeners([MyEntityListener::class])]
4443class MyEntity implements \Stringable
4544{
@@ -135,6 +134,16 @@ class MyEntity implements \Stringable
135134}
136135```
137136
137+ 🗃️ Doctrine can't create [ PostgreSQL GiST indexes] ( https://www.postgresql.org/docs/current/gist.html ) .
138+ Add a GiST index to an ` ltree ` column by manually adding its ` CREATE INDEX `
139+ command to the migration:
140+
141+ ``` sql
142+ // Example GiST index for ltree with a custom signature length (must be a multiple of 4 )
143+ CREATE INDEX my_entity_path_gist_idx
144+ ON my_entity USING GIST (path gist_ltree_ops(siglen = 100 ));
145+ ```
146+
138147⚠️ ** Important** : Changing an entity's parent requires to cascade the change
139148to all its children.
140149This is not handled automatically by Doctrine.
@@ -150,17 +159,24 @@ when the `path` is present in the changed fields:
150159namespace App\EventListener;
151160
152161use App\Entity\MyEntity;
162+ use Doctrine\Bundle\DoctrineBundle\Attribute\AsEntityListener;
153163use Doctrine\ORM\Event\PreUpdateEventArgs;
154- use Doctrine\ORM\Mapping as ORM ;
164+ use Doctrine\ORM\Events ;
155165
166+ #[AsEntityListener(event: Events::preUpdate, method: 'preUpdate', entity: MyEntity::class)]
156167final readonly class MyEntityListener
157168{
158- #[ORM\PreUpdate]
159169 public function preUpdate(MyEntity $entity, PreUpdateEventArgs $eventArgs): void
160170 {
161171 if ($eventArgs->hasChangedField('path')) {
172+ $em = $eventArgs->getObjectManager();
173+ $uow = $em->getUnitOfWork();
174+ $meta = $em->getClassMetadata(MyEntity::class);
175+
162176 foreach($entity->getChildren() as $child) {
163177 $child->setParent($entity);
178+ // Ensure Doctrine picks up the modification
179+ $uow->recomputeSingleEntityChangeSet($meta, $child);
164180 }
165181 }
166182 }
0 commit comments