Star working on category tree
This commit is contained in:
@@ -11,10 +11,11 @@ final class CategoryController extends BaseController
|
|||||||
#[Route('/category/{category}', name: 'app_category')]
|
#[Route('/category/{category}', name: 'app_category')]
|
||||||
public function __invoke(string $category): Response
|
public function __invoke(string $category): Response
|
||||||
{
|
{
|
||||||
if($this->cache->getItem('list_category_'.$category)->isHit()) {
|
// if($this->cache->getItem('list_category_'.$category)->isHit()) {
|
||||||
return $this->render('productList.html.twig', ['listType' => 'category_'.$category]);
|
// return $this->render('productList.html.twig', ['listType' => 'category_'.$category]);
|
||||||
}
|
// }
|
||||||
|
|
||||||
|
/** @var Product[] $products */
|
||||||
$products = Product::with(['price', 'lowestPrice'])
|
$products = Product::with(['price', 'lowestPrice'])
|
||||||
->selectRaw('products.*')
|
->selectRaw('products.*')
|
||||||
->distinct('products.id')
|
->distinct('products.id')
|
||||||
@@ -24,6 +25,61 @@ final class CategoryController extends BaseController
|
|||||||
->orderByDesc('created_by')
|
->orderByDesc('created_by')
|
||||||
->get();
|
->get();
|
||||||
|
|
||||||
return $this->render('productList.html.twig', ['products' => $products, 'listType' => 'category_'.$category]);
|
$categoriesTree = [];
|
||||||
|
|
||||||
|
foreach ($products as $product) {
|
||||||
|
$categoriesTree = $this->addToTree($product->categories, $categoriesTree);
|
||||||
|
dump($categoriesTree);
|
||||||
|
}
|
||||||
|
|
||||||
|
dd($categoriesTree);
|
||||||
|
|
||||||
|
dd();
|
||||||
|
|
||||||
|
return $this->render('productList.html.twig', ['products' => $products, 'listType' => 'category_' . $category]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function addToTree(array $categories, mixed $categoriesTree)
|
||||||
|
{
|
||||||
|
// $tree = [
|
||||||
|
// "Elektronarzędzia" => [
|
||||||
|
// "Maszyny i narzędzia do cięcia" => [
|
||||||
|
// "Pilarki tarczowe" => []
|
||||||
|
//
|
||||||
|
// ]
|
||||||
|
// ]
|
||||||
|
// ];
|
||||||
|
// dump($tree);
|
||||||
|
// $test = &$tree['Elektronarzędzia'];
|
||||||
|
// $test = &$test['Maszyny i narzędzia do cięcia'];
|
||||||
|
// $test = &$test['Maszyny i narzędzia do cięcia2'];
|
||||||
|
// $test = &$test['Maszyny i narzędzia do cięcia3'];
|
||||||
|
// $test['node'] = [];
|
||||||
|
// dump(
|
||||||
|
// $tree
|
||||||
|
// );
|
||||||
|
//
|
||||||
|
// $tree2 = [];
|
||||||
|
// $tree2['Elektronarzędzia'] = [];
|
||||||
|
// $tree2['Elektronarzędzia']['Maszyny i narzędzia do cięcia'] = [];
|
||||||
|
|
||||||
|
|
||||||
|
$tmp = &$categoriesTree;
|
||||||
|
foreach ($categories as $category) {
|
||||||
|
|
||||||
|
if (empty($tmp[$category])) {
|
||||||
|
$tmp[$category] = [];
|
||||||
|
$tmp = &$tmp[$category];
|
||||||
|
} else {
|
||||||
|
$tmp = &$tmp[$category];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
dump($categoriesTree);
|
||||||
|
|
||||||
|
|
||||||
|
return $categoriesTree;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user