feature/category-tree
Co-authored-by: Krzysiej <krzysiej@gmail.com> Co-committed-by: Krzysiej <krzysiej@gmail.com>
This commit was merged in pull request #55.
This commit is contained in:
@@ -11,10 +11,11 @@ final class CategoryController extends BaseController
|
||||
#[Route('/category/{category}', name: 'app_category')]
|
||||
public function __invoke(string $category): Response
|
||||
{
|
||||
if($this->cache->getItem('list_category_'.$category)->isHit()) {
|
||||
return $this->render('productList.html.twig', ['listType' => 'category_'.$category]);
|
||||
}
|
||||
// if($this->cache->getItem('list_category_'.$category)->isHit()) {
|
||||
// return $this->render('productList.html.twig', ['listType' => 'category_'.$category]);
|
||||
// }
|
||||
|
||||
/** @var Product[] $products */
|
||||
$products = Product::with(['price', 'lowestPrice'])
|
||||
->selectRaw('products.*')
|
||||
->distinct('products.id')
|
||||
@@ -24,6 +25,26 @@ final class CategoryController extends BaseController
|
||||
->orderByDesc('created_by')
|
||||
->get();
|
||||
|
||||
return $this->render('productList.html.twig', ['products' => $products, 'listType' => 'category_'.$category]);
|
||||
$categoriesTree = [];
|
||||
|
||||
foreach ($products as $product) {
|
||||
$categoriesTree = $this->addToTree($product->categories, $categoriesTree);
|
||||
}
|
||||
|
||||
return $this->render('productList.html.twig', ['products' => $products, 'listType' => 'category_' . $category, 'category' => $category, 'categoryTree' => $categoriesTree]);
|
||||
}
|
||||
|
||||
private function addToTree(array $categories, mixed $categoriesTree)
|
||||
{
|
||||
$tmp = &$categoriesTree;
|
||||
foreach ($categories as $category) {
|
||||
if (empty($tmp[$category])) {
|
||||
$tmp[$category] = ['count' => 0];
|
||||
}
|
||||
$tmp = &$tmp[$category];
|
||||
$tmp['count']++;
|
||||
}
|
||||
|
||||
return $categoriesTree;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user