123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- <?php
- namespace WebserviceBundle\Controller;
- use FOS\RestBundle\Controller\Annotations\QueryParam;
- use FOS\RestBundle\Controller\Annotations\RouteResource;
- use FOS\RestBundle\Controller\Annotations\View;
- use FOS\RestBundle\Request\ParamFetcherInterface;
- use FOS\RestBundle\Util\Codes;
- use FOS\RestBundle\View\View as FOSView;
- use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
- use Symfony\Bundle\FrameworkBundle\Controller\Controller;
- use Symfony\Component\Form\Form;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\HttpFoundation\Response;
- use Voryx\RESTGeneratorBundle\Controller\VoryxController;
- class RESTController extends VoryxController
- {
- var $repository = 'ClientBundle:Client';
-
- /**
- * Get all entities.
- *
- * @View(serializerEnableMaxDepthChecks=true)
- *
- * @param ParamFetcherInterface $paramFetcher
- * @param string $repository
- *
- * @return Response
- *
- * @QueryParam(name="offset", requirements="\d+", nullable=true, description="Offset from which to start listing notes.")
- * @QueryParam(name="limit", requirements="\d+", default="20", description="How many notes to return.")
- * @QueryParam(name="order_by", nullable=true, array=true, description="Order by fields. Must be an array ie. &order_by[name]=ASC&order_by[description]=DESC")
- * @QueryParam(name="filters", nullable=true, array=true, description="Filter by fields. Must be an array ie. &filters[id]=3")
- * &filters[qb-criteria] => Utilizará el matching con criteria donde los parámetros filters realizarán "field like %value%".
- */
- public function cgetAction(ParamFetcherInterface $paramFetcher)
- {
- $em = $this->getDoctrine()->getManager();
-
- $filters = !is_null($paramFetcher->get('filters')) ? $paramFetcher->get('filters') : array();
- if(isset($filters['qb-criteria'])) {
- try {
- unset($filters['qb-criteria']);
- $criteria = new \Doctrine\Common\Collections\Criteria();
- foreach($filters as $field => $value) {
- $criteria->andWhere($criteria->expr()->contains("$field", "$value"));
- }
- if(!is_null($paramFetcher->get('offset'))) {
- $criteria->setFirstResult($paramFetcher->get('offset'));
- }
- if(!is_null($paramFetcher->get('limit'))) {
- $criteria->setMaxResults($paramFetcher->get('limit'));
- }
- if($paramFetcher->get('order_by')) {
- $order_by = $paramFetcher->get('order_by');
- $orderBy = array();
- foreach($order_by as $field => $order) {
- $orderBy[$field] = $order;
- }
- $criteria->orderBy($orderBy);
- }
- $repo = $em->getRepository($this->repository);
- $entities = $repo->matching($criteria)->toArray();
- if ($entities) {
- return $entities;
- }
-
- } catch (\Exception $e) {
- return FOSView::create($e->getMessage(), Codes::HTTP_INTERNAL_SERVER_ERROR);
- }
-
- } else {
- try {
- $offset = $paramFetcher->get('offset');
- $limit = $paramFetcher->get('limit');
- $order_by = $paramFetcher->get('order_by');
- $entities = $em->getRepository($this->repository)->findBy($filters, $order_by, $limit, $offset);
- if ($entities) {
- return $entities;
- }
- } catch (\Exception $e) {
- return FOSView::create($e->getMessage(), Codes::HTTP_INTERNAL_SERVER_ERROR);
- }
- }
- return array();
- }
-
- }
|