Skip to content

Commit 24f1087

Browse files
committed
trying to solve circular reference when creating entity
1 parent 6f1f016 commit 24f1087

File tree

4 files changed

+63
-10
lines changed

4 files changed

+63
-10
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Spameri\Elastic\Entity;
4+
5+
interface STIElasticEntityInterface
6+
{
7+
8+
}

src/Factory/EntityFactory.php

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ class: $class,
3838
return $entity;
3939
}
4040

41+
$this->identityMap->creatingEntityList[$class][$hit->id()] = true;
42+
4143
$properties = $this->resolveProperties(
4244
hit: $hit,
4345
class: $class,
@@ -52,6 +54,15 @@ class: $class,
5254

5355
$this->identityMap->add($entity);
5456

57+
if (isset($this->identityMap->uninitializedEntityList[$class][$hit->id()]) === true) {
58+
foreach ($this->identityMap->uninitializedEntityList[$class][$hit->id()] as $propertyName => $notCompletedEntity) {
59+
$notCompletedEntity->$propertyName = $entity;
60+
unset($this->identityMap->uninitializedEntityList[$class][$hit->id()]);
61+
}
62+
63+
unset($this->identityMap->creatingEntityList[$class][$hit->id()]);
64+
}
65+
5566
return $entity;
5667
}
5768

@@ -192,10 +203,17 @@ class: $propertyTypeName,
192203
} elseif (
193204
$attribute->getName() === \Spameri\Elastic\Mapping\STIElasticEntity::class
194205
) {
195-
$propertyValue = $entityManager->find(
196-
id: $value[\Spameri\Elastic\Model\Insert\PrepareEntityArray::ENTITY_ID],
197-
class: $value[\Spameri\Elastic\Model\Insert\PrepareEntityArray::ENTITY_CLASS],
198-
);
206+
if (isset($this->identityMap->creatingEntityList[$value[\Spameri\Elastic\Model\Insert\PrepareEntityArray::ENTITY_CLASS]][$value[\Spameri\Elastic\Model\Insert\PrepareEntityArray::ENTITY_ID]])) {
207+
$parentClass = $value[\Spameri\Elastic\Model\Insert\PrepareEntityArray::ENTITY_CLASS];
208+
$propertyValue = eval("return (new class() extends $parentClass {public function __construct(){}});");
209+
$this->identityMap->uninitializedEntityList[$parentClass][$value[\Spameri\Elastic\Model\Insert\PrepareEntityArray::ENTITY_ID]][$property->getName()] = $propertyValue;
210+
211+
} else {
212+
$propertyValue = $entityManager->find(
213+
id: $value[\Spameri\Elastic\Model\Insert\PrepareEntityArray::ENTITY_ID],
214+
class: $value[\Spameri\Elastic\Model\Insert\PrepareEntityArray::ENTITY_CLASS],
215+
);
216+
}
199217

200218
$this->changeSet->markExisting($propertyValue);
201219

@@ -218,10 +236,16 @@ class: $value[\Spameri\Elastic\Model\Insert\PrepareEntityArray::ENTITY_CLASS],
218236
isset(\class_implements($propertyTypeName)[\Spameri\Elastic\Entity\ElasticEntityInterface::class]) === true
219237
&& \is_string($value) === true
220238
) {
221-
$propertyValue = $entityManager->find(
222-
id: $value,
223-
class: $propertyTypeName,
224-
);
239+
if (isset($this->identityMap->creatingEntityList[$propertyTypeName][$value])) {
240+
$propertyValue = eval("return (new class() extends $propertyTypeName {public function __construct(){}});");
241+
$this->identityMap->uninitializedEntityList[$propertyTypeName][$value][$property->getName()] = $propertyValue;
242+
243+
} else {
244+
$propertyValue = $entityManager->find(
245+
id: $value,
246+
class: $propertyTypeName,
247+
);
248+
}
225249

226250
} elseif ($this->container->getByType($propertyTypeName, false) !== null) {
227251
$propertyValue = $this->container->getByType($propertyTypeName);

src/Mapping/STIElasticEntity.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Spameri\Elastic\Mapping;
4+
5+
#[\Attribute(\Attribute::TARGET_PROPERTY|\Attribute::TARGET_PARAMETER)]
6+
class STIElasticEntity
7+
{
8+
9+
}

src/Model/IdentityMap.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,27 @@ class IdentityMap
66
{
77

88
/**
9-
* @var array<string, array<string, \Spameri\Elastic\Entity\AbstractElasticEntity>>
9+
* @var array<class-string, array<string, \Spameri\Elastic\Entity\AbstractElasticEntity>>
1010
*/
1111
public array $identityMap = [];
1212

1313
/**
14-
* @var array<string, array<string, string>>
14+
* @var array<class-string, array<string, string>>
1515
*/
1616
public array $persisted = [];
1717

18+
/**
19+
* @var array<class-string, array<string, bool>>
20+
*/
21+
public array $creatingEntityList = [];
22+
23+
/**
24+
* entity that is missing, id, propertyName, entity that has missing entity in property
25+
*
26+
* @var array<class-string, array<string, array<string, \Spameri\Elastic\Entity\AbstractElasticEntity>>>
27+
*/
28+
public array $uninitializedEntityList = [];
29+
1830

1931
public function add(
2032
\Spameri\Elastic\Entity\AbstractElasticEntity $entity,

0 commit comments

Comments
 (0)