Skip to content

Commit 5ec678b

Browse files
committed
Implement Doctrine discriminator maps for dynamic entity management
1 parent 9213c77 commit 5ec678b

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

src/Services/MeilisearchService.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ public function __construct(NormalizerInterface $normalizer, Engine $engine, arr
5757

5858
public function isSearchable($className): bool
5959
{
60-
if (is_object($className)) {
61-
$className = ClassUtils::getClass($className);
62-
}
60+
$className = $this->getBaseClassName($className);
6361

6462
return in_array($className, $this->searchableEntities, true);
6563
}
@@ -76,6 +74,8 @@ public function getConfiguration(): Collection
7674

7775
public function searchableAs(string $className): string
7876
{
77+
$className = $this->getBaseClassName($className);
78+
7979
$indexes = new Collection($this->getConfiguration()->get('indices'));
8080
$index = $indexes->firstWhere('class', $className);
8181

@@ -207,7 +207,8 @@ public function count(string $className, string $query = '', array $searchParams
207207

208208
public function shouldBeIndexed(object $entity): bool
209209
{
210-
$className = ClassUtils::getClass($entity);
210+
$className = $this->getBaseClassName($entity);
211+
211212
$propertyPath = $this->indexIfMapping[$className];
212213

213214
if (null !== $propertyPath) {
@@ -221,6 +222,21 @@ public function shouldBeIndexed(object $entity): bool
221222
return true;
222223
}
223224

225+
private function getBaseClassName(mixed $object_or_class): string
226+
{
227+
foreach ($this->searchableEntities as $class) {
228+
if (is_a($object_or_class, $class, true)) {
229+
return $class;
230+
}
231+
}
232+
233+
if (is_object($object_or_class)) {
234+
return ClassUtils::getClass($object_or_class);
235+
}
236+
237+
return $object_or_class;
238+
}
239+
224240
private function setSearchableEntities(): void
225241
{
226242
$searchable = [];
@@ -313,7 +329,7 @@ private function makeSearchServiceResponseFrom(
313329
foreach (array_chunk($entities, $this->configuration->get('batchSize')) as $chunk) {
314330
$searchableEntitiesChunk = [];
315331
foreach ($chunk as $entity) {
316-
$entityClassName = ClassUtils::getClass($entity);
332+
$entityClassName = $this->getBaseClassName($entity);
317333

318334
$searchableEntitiesChunk[] = new SearchableEntity(
319335
$this->searchableAs($entityClassName),

0 commit comments

Comments
 (0)