Browse Source

Agregado para que el webservice pueda filtrar por id en los objectos/relaciones

Gabriel Gosparo 6 years ago
parent
commit
3ef8fb9ba6
1 changed files with 12 additions and 4 deletions
  1. 12 4
      Controller/RESTController.php

+ 12 - 4
Controller/RESTController.php

@@ -43,7 +43,7 @@ abstract class RESTController extends VoryxController
      * @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%".
+     * &filters[qb-criteria] => Utilizará el matching con criteria donde los parámetros filters realizarán "field like %value%".
      * &filters[qb-ids] => Require qb-criteria. This option filter by ids separated by ','. ie. &filters[qb-ids]=1,2,8,9.
      */
     public function cgetAction(ParamFetcherInterface $paramFetcher)
@@ -81,10 +81,18 @@ abstract class RESTController extends VoryxController
                 } else {
                     foreach ($filters as $field => $value) {
                         if ($orWhere) {
-                            $criteria->orWhere($criteria->expr()->contains("$field", "$value"));
+                            if (is_numeric($value)) {                              
+                                $criteria->orWhere($criteria->expr()->in("$field", (is_array($value) ? $value : [$value])));
+                            } else {
+                                $criteria->orWhere($criteria->expr()->contains("$field", "$value"));
+                            }
                         } else {
-                            $criteria->andWhere($criteria->expr()->contains("$field", "$value"));
-                        }
+                            if (is_numeric($value)) {
+                                $criteria->andWhere($criteria->expr()->in("$field", (is_array($value) ? $value : [$value])));
+                            } else {
+                                $criteria->andWhere($criteria->expr()->contains("$field", "$value"));
+                            }
+                        }                    
                     }
                     
                     if (!is_null($paramFetcher->get('limit'))) {