mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
Replaced most of the invocations of getDoctrine()->getManager() with ManagerRegistry->getManager(), and added ManagerRegistry injection to controllers where needed. This is part of an ongoing effort to improve code clarity, and avoid unnecessary method chaining in various parts of the codebase.
53 lines
1.7 KiB
PHP
53 lines
1.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/*
|
|
* Chill is a software for social workers
|
|
*
|
|
* For the full copyright and license information, please view
|
|
* the LICENSE file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Chill\MyBundle\Controller;
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
|
|
|
class example extends \Symfony\Bundle\FrameworkBundle\Controller\AbstractController
|
|
{
|
|
public function __construct(private readonly \Doctrine\Persistence\ManagerRegistry $managerRegistry)
|
|
{
|
|
}
|
|
public function yourAction()
|
|
{
|
|
$em = $this->managerRegistry->getManager();
|
|
// first, get the number of total item are available
|
|
$total = $em
|
|
->createQuery('SELECT COUNT (item.id) FROM ChillMyBundle:Item item')
|
|
->getSingleScalarResult();
|
|
|
|
// get the PaginatorFactory
|
|
$paginatorFactory = $this->get('chill_main.paginator_factory');
|
|
|
|
// create a pagination instance. This instance is only valid for
|
|
// the current route and parameters
|
|
$paginator = $paginatorFactory->create($total);
|
|
|
|
// launch your query on item. Limit the query to the results
|
|
// for the current page using the paginator
|
|
$items = $em->createQuery('SELECT item FROM ChillMyBundle:Item item WHERE <your clause>')
|
|
// use the paginator to get the first item number
|
|
->setFirstResult($paginator->getCurrentPage()->getFirstItemNumber())
|
|
// use the paginator to get the number of items to display
|
|
->setMaxResults($paginator->getItemsPerPage());
|
|
|
|
return $this->render(
|
|
'ChillMyBundle:Item:list.html.twig',
|
|
[
|
|
'items' => $items,
|
|
'paginator' => $paginator,
|
|
]
|
|
);
|
|
}
|
|
}
|