Skip to content

Commit

Permalink
Merge pull request opencart#46 from chrisatomix/master
Browse files Browse the repository at this point in the history
Improved SQL Performance of Category Module
  • Loading branch information
danielkerr committed Aug 14, 2012
2 parents bf4fc74 + 3ded447 commit 3a067a0
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 25 deletions.
70 changes: 46 additions & 24 deletions upload/catalog/controller/module/category.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,38 +29,59 @@ protected function index($setting) {

$this->data['categories'] = array();

$categories = $this->model_catalog_category->getCategories(0);
$categories = $this->model_catalog_category->getCategories(-1); // Select all categories

foreach ($categories as $category) {
$total = $this->model_catalog_product->getTotalProducts(array('filter_category_id' => $category['category_id']));
$catrefs = array();
$catlist = array();
foreach($categories as $tmp) {

$thisref = &$catrefs[ $tmp['category_id'] ];
$thisref['parent_id'] = $tmp['parent_id'];
$thisref['name'] = $tmp['name'];
$thisref['products'] = $tmp['products'];
$thisref['total'] = $tmp['products'];

if($tmp['parent_id'] == 0) {

$catlist[ $tmp['category_id'] ] = & $thisref;
}
else {

$catrefs[ $tmp['parent_id'] ]['children'][ $tmp['category_id'] ] = &$thisref;
}
}

foreach($catlist as $cat_id=>$category) {

$catlist[$cat_id] = $this->model_catalog_category->setCategoryTotals($category);
}


foreach($catlist as $cat_id=>$category) {

$total = $category['total'];

$children_data = array();

$children = $this->model_catalog_category->getCategories($category['category_id']);
if(!empty($category['children'])) {

foreach ($children as $child) {
$data = array(
'filter_category_id' => $child['category_id'],
'filter_sub_category' => true
);

$product_total = $this->model_catalog_product->getTotalProducts($data);

$total += $product_total;
foreach($category['children'] as $child_id=>$child) {

$children_data[] = array(
'category_id' => $child['category_id'],
'name' => $child['name'] . ($this->config->get('config_product_count') ? ' (' . $product_total . ')' : ''),
'href' => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'])
);
$children_data[] = array(
'category_id' => $child_id,
'name' => $child['name'] . ($this->config->get('config_product_count') ? ' (' . $child['products'] . ')' : ''),
'href' => $this->url->link('product/category', 'path=' . $cat_id . '_' . $child_id)
);
}
}

$this->data['categories'][] = array(
'category_id' => $category['category_id'],
'name' => $category['name'] . ($this->config->get('config_product_count') ? ' (' . $total . ')' : ''),
'children' => $children_data,
'href' => $this->url->link('product/category', 'path=' . $category['category_id'])
);
'category_id' => $cat_id,
'name' => $category['name'] . ($this->config->get('config_product_count') ? ' (' . $category['total'] . ')' : ''),
'children' => $children_data,
'href' => $this->url->link('product/category', 'path=' . $cat_id)
);

}

if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/category.tpl')) {
Expand All @@ -71,5 +92,6 @@ protected function index($setting) {

$this->render();
}

}
?>
34 changes: 33 additions & 1 deletion upload/catalog/model/catalog/category.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,42 @@ public function getCategory($category_id) {
}

public function getCategories($parent_id = 0) {
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) LEFT JOIN " . DB_PREFIX . "category_to_store c2s ON (c.category_id = c2s.category_id) WHERE c.parent_id = '" . (int)$parent_id . "' AND cd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND c2s.store_id = '" . (int)$this->config->get('config_store_id') . "' AND c.status = '1' ORDER BY c.sort_order, LCASE(cd.name)");

$sql = "SELECT *,
(SELECT COUNT(product_id) FROM " . DB_PREFIX . "product_to_category p2c WHERE p2c.category_id = c.category_id) products
FROM " . DB_PREFIX . "category c
LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id)
LEFT JOIN " . DB_PREFIX . "category_to_store c2s ON (c.category_id = c2s.category_id)
WHERE 1 ";
if($parent_id >= 0) {

// Specify '-1' to select all categories
$sql .= "AND c.parent_id = '" . (int)$parent_id . "' ";
}
$sql .= "AND cd.language_id = '" . (int)$this->config->get('config_language_id') . "'
AND c2s.store_id = '" . (int)$this->config->get('config_store_id') . "'
AND c.status = '1'
ORDER BY c.sort_order, LCASE(cd.name)";
$query = $this->db->query($sql);

return $query->rows;
}

public function setCategoryTotals($category) {

$category['total'] = $category['products'];
if (isset($category['children']) && is_array($category['children'])) {

$children = array();

foreach ($category['children'] as $child_id=>$child) {
$children[$child_id] = $this->setCategoryTotals($child);
$category['total'] += $children[$child_id]['total'];
}
$category['children'] = $children;
}
return $category;
}

public function getCategoriesByParentId($category_id) {
$category_data = array();
Expand Down

0 comments on commit 3a067a0

Please sign in to comment.