Commit f8e57532 by Lech Sawon

include simple sorting form

parent fb19fb6b
......@@ -22,12 +22,12 @@ final class CategoryController extends AbstractController
/**
* @Route("/", name="category_index", methods={"GET"})
*/
public function index(CategoryRepository $categoryRepository): Response
public function index(Request $request, CategoryRepository $categoryRepository): Response
{
$this->denyAccessUnlessGranted('ROLE_USER');
$sorting = $request->query->get('sorting');
return $this->render('category/index.html.twig', [
'categories' => $categoryRepository->findAll(),
'categories' => $categoryRepository->findSorted($sorting)
]);
}
......
......@@ -19,4 +19,20 @@ class CategoryRepository extends ServiceEntityRepository
parent::__construct($registry, Category::class);
}
public function findSorted($sorting)
{
$qb = $this
->createQueryBuilder('category');
if ('ASC' === $sorting) {
$qb->addOrderBy('category.name', 'ASC');
} elseif ('DESC' === $sorting) {
$qb->addOrderBy('category.name', 'DESC');
}
$query = $qb->getQuery();
return $query->getResult();
}
}
<form>
<label>
<select name="sorting">
<option value="">Brak</option>
<option value="ASC">Spadająco</option>
<option value="DESC">Rosnąco</option>
</select>
</label>
<input type="submit"/>
</form>
......@@ -4,7 +4,7 @@
{% block body %}
<h1>Category</h1>
{{ include('_embed/_simple_sorting.html.twig') }}
<table class="table">
<thead>
<tr>
......
......@@ -4,16 +4,7 @@
{% block body %}
<h1>Room index</h1>
<form>
<label>
<select name="sorting">
<option value="">Brak</option>
<option value="ASC">Spadająco</option>
<option value="DESC">Rosnąco</option>
</select>
</label>
<input type="submit"/>
</form>
{{ include('_embed/_simple_sorting.html.twig') }}
<table class="table">
<thead>
<tr>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment