@@ -10,6 +10,8 @@ class PrepareEntityArray
1010 */
1111 private $ serviceLocator ;
1212
13+ private array $ insertedEntities ;
14+
1315
1416 public function __construct (
1517 \Spameri \Elastic \Model \ServiceLocatorInterface $ serviceLocator
@@ -26,6 +28,9 @@ public function prepare(
2628 \Spameri \Elastic \Entity \ElasticEntityInterface $ entity
2729 ): array
2830 {
31+ $ this ->insertedEntities = [];
32+ $ this ->insertedEntities [$ entity ->id ()->value ()] = true ;
33+
2934 return $ this ->iterateVariables ($ entity ->entityVariables ());
3035 }
3136
@@ -42,7 +47,13 @@ public function iterateVariables(
4247
4348 foreach ($ variables as $ key => $ property ) {
4449 if ($ property instanceof \Spameri \Elastic \Entity \ElasticEntityInterface) {
45- $ preparedArray [$ key ] = $ this ->serviceLocator ->locate ($ property )->insert ($ property );
50+ if (\in_array ($ property ->id ()->value (), $ this ->insertedEntities )) {
51+ $ preparedArray [$ key ] = $ property ->id ()->value ();
52+
53+ } else {
54+ $ preparedArray [$ key ] = $ this ->serviceLocator ->locate ($ property )->insert ($ property );
55+ $ this ->insertedEntities [$ property ->id ()->value ()] = true ;
56+ }
4657
4758 } elseif ($ property instanceof \Spameri \Elastic \Entity \EntityInterface) {
4859 $ preparedArray [$ key ] = $ this ->iterateVariables ($ property ->entityVariables ());
@@ -66,7 +77,13 @@ public function iterateVariables(
6677 } else {
6778 /** @var \Spameri\Elastic\Entity\ElasticEntityInterface $item */
6879 foreach ($ property as $ item ) {
69- $ preparedArray [$ key ][] = $ this ->serviceLocator ->locate ($ item )->insert ($ item );
80+ if (\in_array ($ item ->id ()->value (), $ this ->insertedEntities )) {
81+ $ preparedArray [$ key ][] = $ item ->id ()->value ();
82+
83+ } else {
84+ $ preparedArray [$ key ][] = $ this ->serviceLocator ->locate ($ item )->insert ($ item );
85+ $ this ->insertedEntities [$ item ->id ()->value ()] = true ;
86+ }
7087 }
7188 }
7289
0 commit comments