refactor count loop for having better performance has mentionned in #193

This commit is contained in:
Manuel Raynaud
2014-01-23 14:43:05 +01:00
parent 7f7e8cb2e2
commit 2cee70a2b7
3 changed files with 10 additions and 8 deletions

View File

@@ -337,7 +337,7 @@ abstract class BaseLoop
* @param $pagination * @param $pagination
* @return LoopResult * @return LoopResult
*/ */
public function exec(&$pagination) public function exec(&$pagination, $count = false)
{ {
if ($this instanceof PropelSearchLoopInterface) { if ($this instanceof PropelSearchLoopInterface) {
$searchModelCriteria = $this->buildModelCriteria(); $searchModelCriteria = $this->buildModelCriteria();
@@ -361,6 +361,10 @@ abstract class BaseLoop
} }
} }
if ($count) {
return $results ? count($results) : 0;
}
$loopResult = new LoopResult($results); $loopResult = new LoopResult($results);
if (true === $this->countable) { if (true === $this->countable) {

View File

@@ -91,9 +91,7 @@ class TheliaLoop extends AbstractSmartyPlugin
$dummy = null; $dummy = null;
$loopResults = $loop->exec($dummy); return $loop->exec($dummy, true);
return $loopResults->valid() ? $loopResults->getCount() : 0;
} }
/** /**

View File

@@ -367,18 +367,18 @@ try {
//categories and products //categories and products
$productIdList = array(); $productIdList = array();
$categoryIdList = array(); $categoryIdList = array();
for ($i=1; $i<5; $i++) { for ($i=1; $i<20; $i++) {
$category = createCategory($faker, 0, $i, $categoryIdList, $contentIdList); $category = createCategory($faker, 0, $i, $categoryIdList, $contentIdList);
for ($j=1; $j<rand(0, 5); $j++) { for ($j=1; $j<rand(0, 20); $j++) {
$subcategory = createCategory($faker, $category->getId(), $j, $categoryIdList, $contentIdList); $subcategory = createCategory($faker, $category->getId(), $j, $categoryIdList, $contentIdList);
for ($k=0; $k<rand(0, 5); $k++) { for ($k=0; $k<rand(0, 20); $k++) {
createProduct($faker, $subcategory, $k, $template, $productIdList); createProduct($faker, $subcategory, $k, $template, $productIdList);
} }
} }
for ($k=1; $k<rand(1, 6); $k++) { for ($k=1; $k<rand(1, 50); $k++) {
createProduct($faker, $category, $k, $template, $productIdList); createProduct($faker, $category, $k, $template, $productIdList);
} }
} }