Ejemplo n.º 1
0
 /**
  * Read records list
  *
  * @param  Request     $request
  * @param  Application $app
  * @return Response
  * @throws \InvalidArgumentException
  * @throws AccessDeniedHttpException
  * @throws \Twig_Error
  */
 public function indexAction(Request $request, Application $app)
 {
     $manager = $this->getDoctrine()->getManagerForClass($this->definition['entity']);
     // Check access
     if (!$this->isGranted(CrudPermissions::READ)) {
         throw new AccessDeniedHttpException('Access denied for read record(s)');
     }
     // Process order by
     $sort = $request->query->get('sort', 'id');
     $order = $request->query->get('order', 'ASC');
     // Process limit parameter
     $limit = $request->query->getInt('limit', 20);
     // Process offset parameter
     $page = $request->query->getInt('page', 1);
     $this->getEventDispatcher()->dispatch(CrudEvents::BEFORE_LIST, new CrudEvent($this));
     // Build results query
     $qb = QueryBuilderFactory::create($manager, $this->definition['entity']);
     $qb->where($request->query->get('where'));
     $qb->orderBy($sort, $order);
     $qb->setMaxResults($limit);
     $qb->setFirstResult($page * $limit - $limit);
     $records = $qb->getResult();
     $total = $qb->getTotalCount();
     $this->getEventDispatcher()->dispatch(CrudEvents::AFTER_LIST, new CrudEvent($this));
     $this->getEventDispatcher()->dispatch(CrudEvents::BEFORE_RENDER, new CrudEvent($this));
     // Render template
     $view = $this->renderView($this->definition['tpl_list'], ['definition' => $this->definition, 'records' => $records, 'limit' => $limit, 'page' => $page, 'total' => $total]);
     $this->getEventDispatcher()->dispatch(CrudEvents::AFTER_RENDER, new CrudEvent($this));
     return new Response($view);
 }
Ejemplo n.º 2
0
 /**
  * List records
  *
  * @param  Request     $request
  * @param  Application $app
  * @return Response
  * @throws \Exception
  */
 public function indexAction(Request $request, Application $app)
 {
     $manager = $this->getDoctrine()->getManagerForClass($this->definition['entity']);
     if (!$this->isGranted(CrudPermissions::READ)) {
         throw new AccessDeniedHttpException('Access denied for read record(s)');
     }
     // Process order by
     $sort = $request->query->get('sort', 'id');
     $order = $request->query->get('order', 'ASC');
     // Process limit parameter
     $limit = $request->query->getInt('limit', 20);
     // Process offset parameter
     $page = $request->query->getInt('page', 1);
     $this->getEventDispatcher()->dispatch(RestEvents::BEFORE_LIST, new RestEvent($this));
     // Build results query
     $qb = QueryBuilderFactory::create($manager, $this->definition['entity']);
     $qb->where($request->query->get('where'));
     $qb->orderBy($sort, $order);
     $qb->setMaxResults($limit);
     $qb->setFirstResult($page * $limit - $limit);
     $records = $qb->getResult();
     $this->getEventDispatcher()->dispatch(RestEvents::AFTER_LIST, new RestEvent($this));
     return new Response($this->getSerializer()->serialize(['records' => $records], 'json'), 200, $this->defaultHeaders);
 }