Skip to content

Commit 73d4ce1

Browse files
committed
feat: index nested entities
1 parent 7b3e7d1 commit 73d4ce1

File tree

2 files changed

+62
-6
lines changed

2 files changed

+62
-6
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
}
1111
],
1212
"require": {
13-
"php": "^7.4||^8.0",
13+
"php": "8.4.*",
1414
"doctrine/orm": "^2.8 || ^3.2",
1515
"symfony/framework-bundle": "^4.3|^5|^6.0|^7.0",
1616
"symfony/console": "^4.3.4|^5|^6.0|^7.0",

src/Transformer/DoctrineToTypesenseTransformer.php

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
namespace ACSEO\TypesenseBundle\Transformer;
66

7+
use App\Doctrine\Entity\Site\Site;
8+
use Doctrine\Common\Collections\Collection;
79
use Doctrine\Common\Util\ClassUtils;
10+
use Doctrine\ORM\PersistentCollection;
811
use Symfony\Component\DependencyInjection\ContainerInterface;
912
use Symfony\Component\PropertyAccess\Exception\RuntimeException;
1013
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
@@ -61,11 +64,7 @@ public function convert($entity): array
6164
if (str_contains($entityAttribute, '::')) {
6265
$value = $this->getFieldValueFromService($entity, $entityAttribute);
6366
} else {
64-
try {
65-
$value = $this->accessor->getValue($entity, $fieldsInfo['entity_attribute']);
66-
} catch (RuntimeException $exception) {
67-
$value = null;
68-
}
67+
$value = $this->getFieldValueFromEntity($entity, $entityAttribute);
6968
}
7069

7170
$name = $fieldsInfo['name'];
@@ -141,4 +140,61 @@ private function getFieldValueFromService($entity, $entityAttribute)
141140
return null;
142141
}
143142

143+
/*
144+
* @param object $entity The starting entity object.
145+
* @param string $entityAttribute The dot-separated attribute path.
146+
* @return mixed|null The value of the attribute, or null if not found or an exception occurs.
147+
*/
148+
private function getFieldValueFromEntity(object $entity, string $entityAttribute)
149+
{
150+
if (empty($entityAttribute)) {
151+
return null;
152+
}
153+
154+
try {
155+
if ($this->accessor->isReadable($entity, $entityAttribute)) {
156+
return $this->accessor->getValue($entity, $entityAttribute);
157+
}
158+
159+
// Handle cases where the path *might* involve a Collection (1-n, n-n)
160+
$atr = explode('.', $entityAttribute);
161+
$firstProperty = $atr[0];
162+
163+
if ($this->accessor->isReadable($entity, $firstProperty)) {
164+
$nestedValue = $this->accessor->getValue($entity, $firstProperty);
165+
166+
if ($nestedValue instanceof Collection || is_array($nestedValue)) {
167+
$remainingPath = implode('.', array_slice($atr, 1));
168+
169+
if (empty($remainingPath)) {
170+
return $nestedValue;
171+
}
172+
173+
$results = [];
174+
175+
foreach ($nestedValue as $item) {
176+
$result = $this->getFieldValueFromEntity($item, $remainingPath);
177+
if ($result !== null) {
178+
if (is_array($result)) {
179+
$results = array_merge($results, $result);
180+
} else {
181+
$results[] = $result;
182+
}
183+
}
184+
}
185+
186+
if (count($results) > 0) {
187+
return $results;
188+
}
189+
} else {
190+
return null;
191+
}
192+
}
193+
194+
return null;
195+
196+
} catch (RuntimeException $exception) {
197+
return null;
198+
}
199+
}
144200
}

0 commit comments

Comments
 (0)