Skip to content

Commit a156685

Browse files
committed
Add one to one fetch mode to Facades
1 parent 8cae8ec commit a156685

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/LightQL/Sessions/Facade.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,37 @@ private function _fetchManyToOne(&$entity, $property)
411411
}, $results);
412412
}
413413

414+
/**
415+
* @param IEntity $entity
416+
* @param string $property
417+
* @throws \ElementaryFramework\Annotations\Exceptions\AnnotationException
418+
* @throws \ElementaryFramework\LightQL\Exceptions\LightQLException
419+
*/
420+
private function _fetchOneToOne(&$entity, $property)
421+
{
422+
$oneToOne = Annotations::ofProperty($entity, $property, "@oneToOne");
423+
$referencedEntityAnnotations = Annotations::ofClass($oneToOne[0]->entity, "@entity");
424+
$mappedPropertyAnnotation = Annotations::ofProperty($oneToOne[0]->entity, $this->_getReferencePropertyName($this->getEntityClassName()), "@oneToOne");
425+
426+
$lightql = $this->entityManager->getLightQL();
427+
428+
$result = $lightql
429+
->from($referencedEntityAnnotations[0]->table)
430+
->where(array("{$referencedEntityAnnotations[0]->table}.{$oneToOne[0]->referencedColumn}" => $lightql->quote($entity->get($mappedPropertyAnnotation[0]->referencedColumn))))
431+
->selectFirst("{$referencedEntityAnnotations[0]->table}.*");
432+
433+
$propertyName = $this->_getReferencePropertyName($oneToOne[0]->entity);
434+
$className = $oneToOne[0]->entity;
435+
436+
$entity->$propertyName = $result;
437+
438+
if ($result !== null) {
439+
$entity->$propertyName = new $className($result);
440+
$referencedPropertyName = $this->_getReferencePropertyName($this->getEntityClassName());
441+
$entity->{$propertyName}->{$referencedPropertyName} = $entity;
442+
}
443+
}
444+
414445
/**
415446
* @param $rawEntities
416447
* @param $annotations
@@ -437,6 +468,8 @@ private function _parseRawEntities($rawEntities, $annotations): array
437468
$this->_fetchOneToMany($entity, $property->name);
438469
} elseif (Annotations::propertyHasAnnotation($entity, $property->name, "@manyToOne")) {
439470
$this->_fetchManyToOne($entity, $property->name);
471+
} elseif (Annotations::propertyHasAnnotation($entity, $property->name, "@oneToOne")) {
472+
$this->_fetchOneToOne($entity, $property->name);
440473
}
441474
}
442475
}

0 commit comments

Comments
 (0)