Пример #1
0
 /**
  * @param $entity
  * @param array $filter
  * @param bool $empty Esto es para indicar si va a devolver vacio en caso de que no se filtre por nada
  * @return JsonResponse
  */
 public function jsonRead($entity, $filter = array(), $empty = false)
 {
     $repo = $this->getRepo($entity);
     $alias = UtilRepository2::createAlias($entity);
     $query = $this->getParameter('query');
     $qb = $repo->getQB()->select("{$alias}.id", "{$alias}.denominacion");
     $param = $this->getParameter('param');
     $index = $this->getParameter('index');
     $value = $this->getParameter($param)[$index];
     //        ld($this->getParameter('filter'));
     //        ld($index);
     //        ld($param);
     //        ldd($value);
     if ($value != null || $empty == false) {
         //            $filter = array();
         $name = $this->getParameter('filter');
         if (strpos($name, ".") != false) {
             //                ldd('paco');
             $join = explode(".", $name)[0];
             if ($join != $alias) {
                 $qb->join($alias . "." . $join, $join);
             }
         }
         if (is_array($value) && count($value) > 0) {
             $value = array('in', $value);
             $filter[$name] = $value;
         }
         if (!is_array($value)) {
             $filter[$name] = $value;
         }
         //            ldd($filter);
         /**
          * Query debe ser una consulta que devuelve un QueryBuilder
          */
         if ($query) {
             $qb = $repo->{$query}();
         }
         $r = $repo->filterQB($qb, $filter, ResultType::ArrayType);
         $result = array();
         foreach ($r as $row) {
             $result[$row['id']] = $row['denominacion'];
         }
         return new JsonResponse($result);
     }
     return new JsonResponse(array());
 }
Пример #2
0
 /**
  * @author Franlin Rivero Grcia <*****@*****.**>
  * @param EntityManager $em
  * @param string $class
  * @param array $filters Arreglo asociativo campo => valor
  * @param array $foreignFilters Arreglo asociativo asociationName.campo => valor
  * @param ResultType $resultType
  * @param type $order
  * @param type $page
  * @return array
  */
 public static function filterRecordsTableWithAsociations(EntityManager $em, $class, $filters = array(), $order = null, $resultType = ResultType::ObjectType, $page = null)
 {
     $meta = $em->getClassMetadata($class);
     //$fields = $meta->getFieldNames();
     $asocnames = $meta->getAssociationNames();
     $alias = UtilRepository2::createAlias($class);
     $qb = $em->createQueryBuilder()->select($alias)->from($class, $alias);
     $i = 0;
     foreach ($asocnames as $asocname) {
         $qb->addSelect($asocname);
         $join = UtilRepository2Config::$defaultJoinType;
         $qb->{$join}("{$alias}.{$asocname}", $asocname);
     }
     $fields = $meta->getFieldNames();
     UtilRepository2::fieldFilter($alias, $filters, $fields, $asocnames, $meta, $qb);
     UtilRepository2::putOrder($alias, $fields, $order, $qb);
     //       echo $qb->getQuery()->getSQL();die;
     return UtilRepository2::doResult($qb, $resultType, $alias);
 }