src/Controller/AjaxController.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\ItemSpecTypes;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Knp\Component\Pager\PaginatorInterface;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  12. class AjaxController extends AbstractController
  13. {
  14.     #[Route(path'/api/item-spec-types/search'name'api-item-spec-type')]
  15.     public function itemSpecTypes(Request $requestEntityManagerInterface $emPaginatorInterface $paginator): Response
  16.     {
  17.         $search $request->get('term');
  18.         $items $em->getRepository(ItemSpecTypes::class)->createQueryBuilder('i');
  19.         if($search) {
  20.             $items
  21.                 ->where('lower(i.name) LIKE :search or lower(i.adminName) LIKE :search')->setParameter('search'strtolower('%'.$search.'%'));
  22.         }
  23.         $paginator $paginator->paginate($items$request->query->getInt('page'1), 20);
  24.         $return = [];
  25.         foreach ($paginator->getItems() as $item) {
  26.             $return[] = [
  27.                 'text' => $item->getAdminName(),
  28.                 'id' => $item->getId(),
  29.                 'value' => $item->getId(),
  30.             ];
  31.         }
  32.         return new JsonResponse([
  33.             'results' => $return,
  34.         ]);
  35.     }
  36. }