Skip to content

Commit a2eeba6

Browse files
committed
docs: add note for GIST index on 'path' column in MyEntity and fix MyEntityListener preUpdate event handling
1 parent fd62647 commit a2eeba6

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

docs/LTREE-TYPE.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ use Symfony\Bridge\Doctrine\Types\UuidType;
3939
use Symfony\Component\Uid\Uuid;
4040

4141
#[ORM\Entity()]
42-
#[ORM\Index(columns: ['path'])]
4342
#[ORM\EntityListeners([MyEntityListener::class])]
4443
class 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
139148
to all its children.
140149
This is not handled automatically by Doctrine.
@@ -150,17 +159,24 @@ when the `path` is present in the changed fields:
150159
namespace App\EventListener;
151160

152161
use App\Entity\MyEntity;
162+
use Doctrine\Bundle\DoctrineBundle\Attribute\AsEntityListener;
153163
use 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)]
156167
final 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

Comments
 (0)