mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-12 21:34:25 +00:00
[CRUD] make index query more abstract.
Improve build and count query in default index action to be customized in one dedicated method.
This commit is contained in:
parent
28582d033b
commit
590c72ef02
@ -131,3 +131,11 @@ Version 1.5.19
|
||||
|
||||
- [address] add a "homeless" characteristic to addresses ;
|
||||
|
||||
Version 1.5.20
|
||||
==============
|
||||
|
||||
- [CRUD] make index query more abstract.
|
||||
|
||||
Improve build and count query in default index action to be customized
|
||||
in one dedicated method.
|
||||
|
||||
|
@ -276,6 +276,26 @@ class CRUDController extends AbstractController
|
||||
*/
|
||||
protected function onPostIndexFetchQuery(string $action, Request $request, int $totalItems, PaginatorInterface $paginator, $entities) { }
|
||||
|
||||
/**
|
||||
* Build the base query for listing all entities, normally use in a listing
|
||||
* page.
|
||||
*
|
||||
* This base query does not contains any `WHERE` or `SELECT` clauses. Those
|
||||
* are added by other methods, like `queryEntities` and `countQueries`.
|
||||
*
|
||||
* @param string $action
|
||||
* @param Request $request
|
||||
* @return QueryBuilder
|
||||
*/
|
||||
protected function buildQueryEntities(string $action, Request $request)
|
||||
{
|
||||
return $this->getDoctrine()->getManager()
|
||||
->createQueryBuilder()
|
||||
->select('e')
|
||||
->from($this->getEntityClass(), 'e')
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
* Query the entity.
|
||||
*
|
||||
@ -292,17 +312,12 @@ class CRUDController extends AbstractController
|
||||
*/
|
||||
protected function queryEntities(string $action, Request $request, PaginatorInterface $paginator)
|
||||
{
|
||||
$query = $this->getDoctrine()->getManager()
|
||||
->createQueryBuilder()
|
||||
->select('e')
|
||||
->from($this->getEntityClass(), 'e')
|
||||
$query = $this->buildQueryEntities($action, $request)
|
||||
->setFirstResult($paginator->getCurrentPage()->getFirstItemNumber())
|
||||
->setMaxResults($paginator->getItemsPerPage())
|
||||
;
|
||||
->setMaxResults($paginator->getItemsPerPage());
|
||||
|
||||
$this->orderQuery($action, $query, $request, $paginator);
|
||||
|
||||
return $query;
|
||||
// allow to order queries and return the new query
|
||||
return $this->orderQuery($action, $query, $request, $paginator);
|
||||
}
|
||||
|
||||
|
||||
@ -344,8 +359,9 @@ class CRUDController extends AbstractController
|
||||
*/
|
||||
protected function countEntities(string $action, Request $request): int
|
||||
{
|
||||
return $this->getDoctrine()->getManager()
|
||||
->createQuery("SELECT COUNT(e) FROM ".$this->getEntityClass()." e")
|
||||
return $this->buildQueryEntities($action, $request)
|
||||
->select('COUNT(e)')
|
||||
->getQuery()
|
||||
->getSingleScalarResult()
|
||||
;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user