Skip to content

Commit

Permalink
Merge pull request #1001 from BFoucher/feature/data-warmer-nested-ord…
Browse files Browse the repository at this point in the history
…ered-entities

implement OrderBy support in widgetDataWarmer
  • Loading branch information
lenybernard authored Nov 17, 2017
2 parents 1f7c77c + 238dcfb commit be30f2e
Showing 1 changed file with 56 additions and 40 deletions.
96 changes: 56 additions & 40 deletions Bundle/WidgetMapBundle/Warmer/WidgetDataWarmer.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ private function extractAssociatedEntities(array $entities)
) {
//If target Entity is not null, treat it
if ($targetEntity = $this->accessor->getValue($entity, $association['fieldName'])) {
$associatedEntities[$targetClass]['id'][] = new AssociatedEntityToWarm(
$associatedEntities[$targetClass]['id']['unsorted']['entitiesToWarm'][] = new AssociatedEntityToWarm(
AssociatedEntityToWarm::TYPE_MANY_TO_ONE,
$entity,
$association['fieldName'],
Expand All @@ -159,8 +159,14 @@ private function extractAssociatedEntities(array $entities)
$getter = 'get'.ucwords($association['fieldName']);
$entity->$getter()->setDirty(false);
$entity->$getter()->setInitialized(true);
$orderByToken = 'unsorted';

$associatedEntities[$targetClass][$association['mappedBy']][] = new AssociatedEntityToWarm(
if (isset($association['orderBy'])) {
$orderByToken = implode($association['orderBy']);
$associatedEntities[$targetClass][$association['mappedBy']][$orderByToken]['orderBy'] = $association['orderBy'];
}

$associatedEntities[$targetClass][$association['mappedBy']][$orderByToken]['entitiesToWarm'][] = new AssociatedEntityToWarm(
AssociatedEntityToWarm::TYPE_ONE_TO_MANY,
$entity,
$association['fieldName'],
Expand Down Expand Up @@ -196,45 +202,55 @@ private function setAssociatedEntities(array $repositories)
$newEntities = [];

foreach ($repositories as $repositoryName => $findMethods) {
foreach ($findMethods as $findMethod => $associatedEntitiesToWarm) {

//Extract ids to search
$idsToSearch = array_map(function ($associatedEntityToWarm) {
return $associatedEntityToWarm->getEntityId();
}, $associatedEntitiesToWarm);

//Find by id for ManyToOne associations based on target entity id
//Find by mappedBy value for OneToMany associations based on owner entity id
$foundEntities = $this->em->getRepository($repositoryName)->findBy([
$findMethod => array_values($idsToSearch),
]);

/* @var AssociatedEntityToWarm[] $associatedEntitiesToWarm */
foreach ($associatedEntitiesToWarm as $associatedEntityToWarm) {
foreach ($foundEntities as $foundEntity) {
if ($associatedEntityToWarm->getType() === AssociatedEntityToWarm::TYPE_MANY_TO_ONE
&& $foundEntity->getId() === $associatedEntityToWarm->getEntityId()
) {
$inheritorEntity = $associatedEntityToWarm->getInheritorEntity();
$inheritorPropertyName = $associatedEntityToWarm->getInheritorPropertyName();
$this->accessor->setValue($inheritorEntity, $inheritorPropertyName, $foundEntity);
continue;
} elseif ($associatedEntityToWarm->getType() === AssociatedEntityToWarm::TYPE_ONE_TO_MANY
&& $this->accessor->getValue($foundEntity, $findMethod) === $associatedEntityToWarm->getInheritorEntity()
) {
$inheritorEntity = $associatedEntityToWarm->getInheritorEntity();
$inheritorPropertyName = $associatedEntityToWarm->getInheritorPropertyName();
foreach ($findMethods as $findMethod => $groupedBySortAssociatedEntitiesToWarm) {
foreach ($groupedBySortAssociatedEntitiesToWarm as $orderByToken => $groupedAssociatedEntitiesToWarm) {
$associatedEntitiesToWarm = $groupedAssociatedEntitiesToWarm['entitiesToWarm'];

//Extract ids to search
$idsToSearch = array_map(function ($associatedEntityToWarm) {
return $associatedEntityToWarm->getEntityId();
}, $associatedEntitiesToWarm);

$orderBy = [];
if ($orderByToken !== 'unsorted') {
foreach ($groupedAssociatedEntitiesToWarm['orderBy'] as $orderAttribut => $order) {
$orderBy[$orderAttribut] = $order;
}
}

//Don't use Collection getter directly and override Collection
//default behaviour to avoid useless query
$getter = 'get'.ucwords($inheritorPropertyName);
$inheritorEntity->$getter()->add($foundEntity);
$inheritorEntity->$getter()->setDirty(false);
$inheritorEntity->$getter()->setInitialized(true);

//Store new entities to warm if necessary
$newEntities[] = $foundEntity;
continue;
//Find by id for ManyToOne associations based on target entity id
//Find by mappedBy value for OneToMany associations based on owner entity id
$foundEntities = $this->em->getRepository($repositoryName)->findBy([
$findMethod => array_values($idsToSearch),
], $orderBy);

/* @var AssociatedEntityToWarm[] $associatedEntitiesToWarm */
foreach ($associatedEntitiesToWarm as $associatedEntityToWarm) {
foreach ($foundEntities as $foundEntity) {
if ($associatedEntityToWarm->getType() === AssociatedEntityToWarm::TYPE_MANY_TO_ONE
&& $foundEntity->getId() === $associatedEntityToWarm->getEntityId()
) {
$inheritorEntity = $associatedEntityToWarm->getInheritorEntity();
$inheritorPropertyName = $associatedEntityToWarm->getInheritorPropertyName();
$this->accessor->setValue($inheritorEntity, $inheritorPropertyName, $foundEntity);
continue;
} elseif ($associatedEntityToWarm->getType() === AssociatedEntityToWarm::TYPE_ONE_TO_MANY
&& $this->accessor->getValue($foundEntity, $findMethod) === $associatedEntityToWarm->getInheritorEntity()
) {
$inheritorEntity = $associatedEntityToWarm->getInheritorEntity();
$inheritorPropertyName = $associatedEntityToWarm->getInheritorPropertyName();

//Don't use Collection getter directly and override Collection
//default behaviour to avoid useless query
$getter = 'get'.ucwords($inheritorPropertyName);
$inheritorEntity->$getter()->add($foundEntity);
$inheritorEntity->$getter()->setDirty(false);
$inheritorEntity->$getter()->setInitialized(true);

//Store new entities to warm if necessary
$newEntities[] = $foundEntity;
continue;
}
}
}
}
Expand Down

0 comments on commit be30f2e

Please sign in to comment.