Skip to content

Commit

Permalink
Correct index: /<INDEX_NAME>/_doc/<ID> caused failed to parse errors
Browse files Browse the repository at this point in the history
  • Loading branch information
maximehuran committed Jun 2, 2022
1 parent d2930f0 commit 059d865
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/Index/Indexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace MonsieurBiz\SyliusSearchPlugin\Index;

use Doctrine\Common\Proxy\Proxy;
use Doctrine\ORM\EntityManagerInterface;
use Elastica\Document;
use Jane\Component\AutoMapper\AutoMapperInterface;
Expand Down Expand Up @@ -149,6 +150,7 @@ private function indexDocumentable(DocumentableInterface $documentable, ?string

$indexer = $this->clientFactory->getIndexer($documentable, $locale);
foreach ($documentable->getDatasource()->getItems($documentable->getSourceClass()) as $item) {
$item = $this->getRealEntity($item);
if (null !== $locale && $item instanceof TranslatableInterface) {
$item->setCurrentLocale($locale);
}
Expand All @@ -167,4 +169,25 @@ private function getIndexName(DocumentableInterface $documentable, ?string $loca
{
return $documentable->getIndexCode() . strtolower(null !== $locale ? '_' . $locale : '');
}

/**
* Convert proxies classes to the entity one
*
* This avoid to retrieve the incorrect Mapper and have errors like :
* `index: /<INDEX_NAME>/_doc/<ID> caused failed to parse`
*/
private function getRealEntity($entity)
{
if (!$entity instanceof Proxy || !method_exists($entity, 'getId')) {
return $entity;
}

// Clear the entity manager to detach the proxy object
$this->entityManager->clear(get_class($entity));
// Retrieve the original class name
$entityClassName = $this->entityManager->getClassMetadata(get_class($entity))->rootEntityName;
// Find the object in repository from the ID
$object = $this->entityManager->find($entityClassName, $entity->getId());
return $object;
}
}

0 comments on commit 059d865

Please sign in to comment.