Skip to content

Commit

Permalink
Merge pull request #117 from Kunstmaan/fetch_pageparts_performance
Browse files Browse the repository at this point in the history
only do one query for each pagepart type instead of a query per pagepart
  • Loading branch information
jverdeyen committed May 8, 2014
2 parents 720e92b + adedbc9 commit dda587c
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions Repository/PagePartRefRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;

use Kunstmaan\AdminBundle\Entity\DeepCloneInterface;
use Kunstmaan\UtilitiesBundle\Helper\ClassLookup;
use Kunstmaan\PagePartBundle\Helper\PagePartInterface;
Expand Down Expand Up @@ -73,12 +72,37 @@ public function getPagePartRefs(HasPagePartsInterface $page, $context = "main")
public function getPageParts(HasPagePartsInterface $page, $context = "main")
{
$pagepartrefs = $this->getPagePartRefs($page, $context);
$result = array();

// Group pagepartrefs per type and remember the sorting order
$types = $order = array();
$counter = 1;
foreach ($pagepartrefs as $pagepartref) {
$result[] = $pagepartref->getPagePart($this->getEntityManager());
$types[$pagepartref->getPagePartEntityname()][] = $pagepartref->getPagePartId();
$order[$pagepartref->getPagePartEntityname() . $pagepartref->getPagePartId()] = $counter;
$counter++;
}

// Fetch all the pageparts (only one query per pagepart type)
$pageparts = array();
foreach ($types as $classname => $ids) {
$result = $this->getEntityManager()->getRepository($classname)->findBy(array('id' => $ids));
$pageparts = array_merge($pageparts, $result);
}

return $result;
// Order the pageparts
usort($pageparts, function($a, $b) use ($order) {
$aPosition = $order[get_class($a) . $a->getId()];
$bPosition = $order[get_class($b) . $b->getId()];

if ($aPosition < $bPosition) {
return -1;
} elseif ($aPosition > $bPosition) {
return 1;
}
return 0;
});

return $pageparts;
}

/**
Expand Down

0 comments on commit dda587c

Please sign in to comment.