Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow old behavior of having getter function on 1-to-many association without nested Class #202

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 35 additions & 7 deletions Doctrine/Mapper/Factory/DocumentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace FS\SolrBundle\Doctrine\Mapper\Factory;

use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\PersistentCollection;
use FS\SolrBundle\Doctrine\Annotation\Field;
use FS\SolrBundle\Doctrine\Mapper\MetaInformationFactory;
use FS\SolrBundle\Doctrine\Mapper\MetaInformationInterface;
Expand Down Expand Up @@ -59,7 +60,9 @@ public function createDocument(MetaInformationInterface $metaInformation)
}

$fieldValue = $field->getValue();
if (($fieldValue instanceof Collection || is_array($fieldValue)) && $field->nestedClass) {
if (($fieldValue instanceof Collection || is_array($fieldValue)) && !$field->nestedClass) {
$this->mapCollectionField($document, $field, $metaInformation->getEntity());
} elseif (($fieldValue instanceof Collection || is_array($fieldValue)) && $field->nestedClass) {
$this->mapCollectionField($document, $field, $metaInformation->getEntity());
} else if (is_object($fieldValue) && $field->nestedClass) { // index sinsgle object as nested child-document
$document->addField('_childDocuments_', [$this->objectToDocument($fieldValue)], $field->getBoost());
Expand Down Expand Up @@ -94,7 +97,7 @@ private function mapObjectField(Field $field)
if (empty($getter)) {
throw new SolrMappingException(sprintf('Please configure a getter for property "%s" in class "%s"', $field->name, get_class($value)));
}

$getterReturnValue = $this->callGetterMethod($value, $getter);

if (is_object($getterReturnValue)) {
Expand Down Expand Up @@ -152,11 +155,28 @@ private function mapCollectionField($document, Field $field, $sourceTargetObject
$getter = $field->getGetterName();

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

$collection = array_filter($collection, function ($value) {
return $value !== null;
});
if (method_exists($sourceTargetObject, $getter)) {
$collection = $this->callGetterMethod($sourceTargetObject, $getter);

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

if ($collection->count()) {
foreach ($collection->getIterator() as $item) {
$items[] = $this->callGetterMethod($item, $getter);
}

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

}

}

$values = [];
Expand All @@ -169,7 +189,15 @@ private function mapCollectionField($document, Field $field, $sourceTargetObject
}
}

$document->addField('_childDocuments_', $values, $field->getBoost());
if (!method_exists($sourceTargetObject, $getter)) {
foreach ($values as $value) {
$document->addField($field->getNameWithAlias(), $value, $field->getBoost());
}
}
else {
$document->addField('_childDocuments_', $values, $field->getBoost());
}

}

return $values;
Expand Down