Skip to content

Commit

Permalink
do not index empty collections
Browse files Browse the repository at this point in the history
  • Loading branch information
floriansemm committed Nov 15, 2018
1 parent 8e39b0a commit b071b0e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
26 changes: 16 additions & 10 deletions Doctrine/Mapper/Factory/DocumentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,24 +147,30 @@ private function callGetterMethod($object, $getter)
*/
private function mapCollectionField($document, Field $field, $sourceTargetObject)
{
/** @var Collection $value */
$value = $field->getValue();
/** @var Collection $collection */
$collection = $field->getValue();
$getter = $field->getGetterName();

if ($getter != '') {
$value = $this->callGetterMethod($sourceTargetObject, $getter);
$collection = $this->callGetterMethod($sourceTargetObject, $getter);

$collection = array_filter($collection, function ($value) {
return $value !== null;
});
}

$values = [];
foreach ($value as $relatedObj) {
if (is_object($relatedObj)) {
$values[] = $this->objectToDocument($relatedObj);
} else {
$values[] = $relatedObj;
if (count($collection)) {
foreach ($collection as $relatedObj) {
if (is_object($relatedObj)) {
$values[] = $this->objectToDocument($relatedObj);
} else {
$values[] = $relatedObj;
}
}
}

$document->addField('_childDocuments_', $values, $field->getBoost());
$document->addField('_childDocuments_', $values, $field->getBoost());
}

return $values;
}
Expand Down
18 changes: 18 additions & 0 deletions Tests/Doctrine/Mapper/EntityMapperObjectRelationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,24 @@ public function mapRelationFieldByGetter()
$this->assertCollectionItemsMappedProperly($collectionField, 1);
}

/**
* @test
*/
public function doNotIndexEmptyNestedCollection()
{
$collection = new ArrayCollection([]);

$entity = new EntityNestedProperty();
$entity->setId(uniqid());
$entity->setCollectionValidGetter($collection);

$metaInformation = $this->metaInformationFactory->loadInformation($entity);

$document = $this->mapper->toDocument($metaInformation);

$this->assertArrayNotHasKey('_childDocuments_', $document->getFields());
}

/**
* @test
* @expectedException \FS\SolrBundle\Doctrine\Mapper\SolrMappingException
Expand Down

0 comments on commit b071b0e

Please sign in to comment.