Skip to content

Commit ffcfbcf

Browse files
committed
Implement ConditionalUpdate interface for selective entity updates
1 parent 77d6e0f commit ffcfbcf

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/Doctrine/ConditionalUpdate.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSElasticaBundle package.
5+
*
6+
* (c) FriendsOfSymfony <https://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\ElasticaBundle\Doctrine;
13+
14+
interface ConditionalUpdate
15+
{
16+
/**
17+
* Determines if an entity should be updated in Elasticsearch.
18+
*/
19+
public function shouldBeUpdated(): bool;
20+
}

src/Doctrine/Listener.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace FOS\ElasticaBundle\Doctrine;
1313

1414
use Doctrine\Persistence\Event\LifecycleEventArgs;
15+
use FOS\ElasticaBundle\Doctrine\ConditionalUpdate;
1516
use FOS\ElasticaBundle\Persister\ObjectPersister;
1617
use FOS\ElasticaBundle\Persister\ObjectPersisterInterface;
1718
use FOS\ElasticaBundle\Provider\IndexableInterface;
@@ -96,7 +97,9 @@ public function postPersist(LifecycleEventArgs $eventArgs)
9697
$entity = $eventArgs->getObject();
9798

9899
if ($this->objectPersister->handlesObject($entity) && $this->isObjectIndexable($entity)) {
99-
$this->scheduledForInsertion[] = $entity;
100+
if (!$entity instanceof ConditionalUpdate || $entity->shouldBeUpdated()) {
101+
$this->scheduledForInsertion[] = $entity;
102+
}
100103
}
101104
}
102105

@@ -109,7 +112,9 @@ public function postUpdate(LifecycleEventArgs $eventArgs)
109112

110113
if ($this->objectPersister->handlesObject($entity)) {
111114
if ($this->isObjectIndexable($entity)) {
112-
$this->scheduledForUpdate[] = $entity;
115+
if (!$entity instanceof ConditionalUpdate || $entity->shouldBeUpdated()) {
116+
$this->scheduledForUpdate[] = $entity;
117+
}
113118
} else {
114119
// Delete if no longer indexable
115120
$this->scheduleForDeletion($entity);

0 commit comments

Comments
 (0)