Skip to content

Commit

Permalink
- Add support for "to many" relations to be correctly hydrated
Browse files Browse the repository at this point in the history
  • Loading branch information
akadlec committed Nov 18, 2018
1 parent 60a100e commit ed692fc
Showing 1 changed file with 45 additions and 9 deletions.
54 changes: 45 additions & 9 deletions src/IPub/DoctrineCrud/Mapping/EntityMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function fillEntity(Utils\ArrayHash $values, Entities\IEntity $entity, bo
// Nothing to do here
}

foreach (array_merge($reflectionProperties, $classMetadata->getFieldNames(), $classMetadata->getAssociationNames()) as $fieldName) {
foreach (array_unique(array_merge($reflectionProperties, $classMetadata->getFieldNames(), $classMetadata->getAssociationNames())) as $fieldName) {

try {
$propertyReflection = new Nette\Reflection\Property($entityClass, $fieldName);
Expand All @@ -128,7 +128,7 @@ public function fillEntity(Utils\ArrayHash $values, Entities\IEntity $entity, bo

$value = $values->offsetGet($fieldName);

if ($value instanceof Utils\ArrayHash || is_array($value)) {
if (($value instanceof Utils\ArrayHash || is_array($value)) && isset($classMetadata->reflFields[$fieldName])) {
if (!$classMetadata->getFieldValue($entity, $fieldName) instanceof Entities\IEntity) {
$propertyAnnotations = $this->annotationReader->getPropertyAnnotations($propertyReflection);

Expand All @@ -148,20 +148,56 @@ public function fillEntity(Utils\ArrayHash $values, Entities\IEntity $entity, bo
$items = [];

if (is_string($className) && class_exists($className) && ($value instanceof Utils\ArrayHash || is_array($value))) {
$rc = new \ReflectionClass($className);

$subClassIdProperty = NULL;

foreach ($rc->getProperties() as $subClassProperty) {
$subClassPropertyAnnotations = $this->annotationReader->getPropertyAnnotations($subClassProperty);

$subClassAnnotations = array_map((function ($annotation) : string {
return get_class($annotation);
}), $subClassPropertyAnnotations);

if (in_array(ORM\Mapping\Id::class, $subClassAnnotations, TRUE)) {
$subClassIdProperty = $subClassProperty->getName();

break;
}
}

/** @var Utils\ArrayHash $item */
foreach ($value as $item) {
$subEntity = NULL;

$subClassName = $className;

$rc = new \ReflectionClass($className);

if ($rc->isAbstract() && isset($item['entity']) && class_exists($item['entity'])) {
$className = $item['entity'];
$subClassName = $item['entity'];

$rc = new \ReflectionClass($subClassName);
}

if ($subClassIdProperty !== NULL && $item->offsetExists($subClassIdProperty)) {
$subEntity = $this->managerRegistry
->getManagerForClass($subClassName)
->getRepository($subClassName)
->find($item->offsetGet($subClassIdProperty));

$rc = new \ReflectionClass($className);
if ($subEntity !== NULL) {
$subEntity = $this->fillEntity($item, $subEntity);
}
}

if ($constructor = $rc->getConstructor()) {
$subEntity = $rc->newInstanceArgs(Helpers::autowireArguments($constructor, array_merge((array) $item, [$entity])));
if ($subEntity === NULL) {
if ($constructor = $rc->getConstructor()) {
$subEntity = $rc->newInstanceArgs(Helpers::autowireArguments($constructor, array_merge((array) $item, [$entity])));

} else {
$subEntity = new $className;
} else {
$subEntity = new $subClassName;
}
}

$items[] = $subEntity;
Expand Down Expand Up @@ -253,7 +289,7 @@ private function setFieldValue(ORM\Mapping\ClassMetadata $classMetadata, Entitie
// Try to call entity setter
call_user_func_array([$entity, $methodName], [$value]);

} else {
} elseif (isset($classMetadata->reflFields[$field])) {
// Fallback for missing setter
$classMetadata->setFieldValue($entity, $field, $value);
}
Expand Down

0 comments on commit ed692fc

Please sign in to comment.