src/Repository/BaseRepository.php line 57

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use Doctrine\ORM\EntityRepository;
  4. /**
  5.  * Repository
  6.  *
  7.  */
  8. class BaseRepository extends EntityRepository
  9. {
  10.     public function findFiltered(array $filters = array(), $order = array(), $limit null$offset 0$options = array())
  11.     {
  12.         // Grab the querybuilder
  13.         $queryBuilder $this->buildQuery($filters);
  14.         // Order
  15.         if(count($order))
  16.         {
  17.             foreach($order AS $k => $v)
  18.                 $queryBuilder->addOrderBy($k$v);
  19.         }
  20.         // Limit
  21.         if($limit)
  22.             $queryBuilder->setMaxResults($limit);
  23.         // Offset
  24.         if($offset)
  25.             $queryBuilder->setFirstResult($offset);
  26.         // Get the query
  27.         $query $queryBuilder->getQuery();
  28.         // Execute and return
  29.         return $query->getResult();
  30.     }
  31.     public function findOneFiltered(array $filters = array(), $order = array())
  32.     {
  33.         // Grab the querybuilder
  34.         $queryBuilder $this->buildQuery($filters);
  35.         $queryBuilder->setMaxResults(1);
  36.         // Order
  37.         if(count($order))
  38.         {
  39.             foreach($order AS $k => $v)
  40.                 $queryBuilder->addOrderBy($k$v);
  41.         }
  42.         // Get the query
  43.         $query $queryBuilder->getQuery();
  44.         // Execute and return
  45.         return $query->getOneOrNullResult();
  46.     }
  47.     public function findFilteredQueryBuilder(array $filters = array(), $order = array())
  48.     {
  49.         // Grab the querybuilder
  50.         $queryBuilder $this->buildQuery($filters);
  51.         // Order
  52.         if(count($order))
  53.         {
  54.             foreach($order AS $k => $v)
  55.                 $queryBuilder->addOrderBy($k$v);
  56.         }
  57.         // Get the query
  58.         return $queryBuilder;
  59.     }
  60.     public function findFilteredQuery(array $filters = array(), $order = array())
  61.     {
  62.         return $this->findFilteredQueryBuilder($filters$order)->getQuery();
  63.     }
  64.     public function buildDefaultQueryBuilder($queryBuilder$alias$filters)
  65.     {
  66.         // Get the class meta data
  67.         $metaData $this->getEntityManager()->getClassMetadata($this->getClassName());
  68.         // Extract the field mappings
  69.         $fieldMappings $metaData->fieldMappings;
  70.         // Extract the association mappings
  71.         $associationMappings $metaData->associationMappings;
  72.         // Loop the parent classes
  73.         foreach($metaData->parentClasses AS $someClass)
  74.         {
  75.             // Get the class meta data
  76.             $classMetaData $this->getEntityManager()->getClassMetadata($someClass);
  77.             // Merge the field mappings
  78.             foreach($classMetaData->fieldMappings AS $k  => $v)
  79.             {
  80.                 // Add it in if not already got it
  81.                 if(!array_key_exists($k$fieldMappings))
  82.                     $fieldMappings[$k] = $v;
  83.             }
  84.             // Merge the association mappings
  85.             foreach($classMetaData->associationMappings AS $k  => $v)
  86.             {
  87.                 // Add it in if not already got it
  88.                 if(!array_key_exists($k$associationMappings))
  89.                     $associationMappings[$k] = $v;
  90.             }
  91.         }
  92.         // Loop the field mappings
  93.         foreach($fieldMappings AS $key => $someMappedField)
  94.         {
  95.             // Got this in our filter?
  96.             if(array_key_exists($key$filters))
  97.             {
  98.                 // Grab the filter
  99.                 $theFilter $filters[$key];
  100.                 // Basic filter match?
  101.                 if(!is_array($theFilter))
  102.                 {
  103.                     // Is it null?
  104.                     if(is_null($theFilter))
  105.                     {
  106.                         $queryBuilder->andWhere($this->alias '.' $key ' IS NULL');
  107.                     }
  108.                     else
  109.                     {
  110.                         $queryBuilder->andWhere($this->alias '.' $key ' = :' $key)
  111.                             ->setParameter($key$theFilter);
  112.                     }
  113.                 }
  114.                 // Complex filter match
  115.                 else
  116.                 {
  117.                     // Extract the bits
  118.                     list($type$data) = $theFilter;
  119.                     // Deal with in, not_in
  120.                     if(in_array($type, array("in""not_in")))
  121.                     {
  122.                         // What type
  123.                         if($type == "in")
  124.                         {
  125.                             $queryBuilder->andWhere($this->alias '.' $key ' IN(:' $key ')')
  126.                                 ->setParameter($key$data);
  127.                         }
  128.                         elseif($type == "not_in")
  129.                         {
  130.                             $queryBuilder->andWhere($this->alias '.' $key ' NOT IN(:' $key ')')
  131.                                 ->setParameter($key$data);
  132.                         }
  133.                     }
  134.                     // Deal with nulls
  135.                     elseif(in_array($type, array("is_null""not_null")))
  136.                     {
  137.                         // What type
  138.                         if($type == "is_null" && is_bool($data))
  139.                         {
  140.                             if($data)
  141.                                 $queryBuilder->andWhere($this->alias '.' $key ' IS NULL');
  142.                             else
  143.                                 $queryBuilder->andWhere($this->alias '.' $key ' IS NOT NULL');
  144.                         }
  145.                         elseif($type == "not_null" && is_bool($data))
  146.                         {
  147.                             if($data)
  148.                                 $queryBuilder->andWhere($this->alias '.' $key ' IS NOT NULL');
  149.                             else
  150.                                 $queryBuilder->andWhere($this->alias '.' $key ' IS NULL');
  151.                         }
  152.                         else
  153.                             throw new \Exception("Invalid query paramater 'null' value(s)");
  154.                     }
  155.                     // Deal with numeric gt, gte, lt, lte
  156.                     elseif(in_array($someMappedField['type'], array("integer""decimal")) && in_array($type, array("lt""lte""gt""gte")))
  157.                     {
  158.                         // Make sure data is numeric
  159.                         if(!is_numeric($data))
  160.                             throw new \Exception("Cannot use non-numeric data for '" $type "' query");
  161.                         // Less than
  162.                         if($type == "lt")
  163.                         {
  164.                             $queryBuilder->andWhere($this->alias '.' $key ' < :' $key)
  165.                                 ->setParameter($key$data);
  166.                         }
  167.                         // Less than or equal
  168.                         elseif($type == "lte")
  169.                         {
  170.                             $queryBuilder->andWhere($this->alias '.' $key ' <= :' $key)
  171.                                 ->setParameter($key$data);
  172.                         }
  173.                         // Greater than
  174.                         elseif($type == "gt")
  175.                         {
  176.                             $queryBuilder->andWhere($this->alias '.' $key ' > :' $key)
  177.                                 ->setParameter($key$data);
  178.                         }
  179.                         // Greater than or equal
  180.                         elseif($type == "gte")
  181.                         {
  182.                             $queryBuilder->andWhere($this->alias '.' $key ' >= :' $key)
  183.                                 ->setParameter($key$data);
  184.                         }
  185.                     }
  186.                     // Deal with datetimes gt, gte, lt, lte
  187.                     elseif(in_array($someMappedField['type'], array("datetime")) && in_array($type, array("lt""lte""gt""gte")))
  188.                     {
  189.                         // Make sure data is a datetime
  190.                         if(!$data instanceof \DateTime)
  191.                             throw new \Exception("Data must be a DateTime instance for '" $type "' query");
  192.                         // Less than
  193.                         if($type == "lt")
  194.                         {
  195.                             $queryBuilder->andWhere($this->alias '.' $key ' < :' $key)
  196.                                 ->setParameter($key$data->format('Y-m-d H:i:s'));
  197.                         }
  198.                         // Less than or equal
  199.                         elseif($type == "lte")
  200.                         {
  201.                             $queryBuilder->andWhere($this->alias '.' $key ' <= :' $key)
  202.                                 ->setParameter($key$data->format('Y-m-d H:i:s'));
  203.                         }
  204.                         // Greater than
  205.                         elseif($type == "gt")
  206.                         {
  207.                             $queryBuilder->andWhere($this->alias '.' $key ' > :' $key)
  208.                                 ->setParameter($key$data->format('Y-m-d H:i:s'));
  209.                         }
  210.                         // Greater than or equal
  211.                         elseif($type == "gte")
  212.                         {
  213.                             $queryBuilder->andWhere($this->alias '.' $key ' >= :' $key)
  214.                                 ->setParameter($key$data->format('Y-m-d H:i:s'));
  215.                         }
  216.                     }
  217.                     else
  218.                     {
  219.                         throw new \Exception("Invalid query paramater of type '" $type "' on field '" $key "'. Available options: in, not_in, lt, lte, gt, gte, is_null, not_null");
  220.                     }
  221.                 }
  222.             }
  223.         }
  224.         // Loop the association mappings
  225.         foreach($associationMappings AS $key => $someMappedAssociation)
  226.         {
  227.             // Got this in our filter?
  228.             if(array_key_exists($key$filters))
  229.             {
  230.                 // Grab the filter
  231.                 $theFilter $filters[$key];
  232.                 // Basic filter match?
  233.                 if(!is_array($theFilter))
  234.                 {
  235.                     // Is it null?
  236.                     if(is_null($theFilter))
  237.                     {
  238.                         $queryBuilder->andWhere($this->alias '.' $key ' IS NULL');
  239.                     }
  240.                     else
  241.                     {
  242.                         $queryBuilder->andWhere($this->alias '.' $key ' = :' $key)
  243.                             ->setParameter($key$theFilter);
  244.                     }
  245.                 }
  246.                 // Complex filter match
  247.                 else
  248.                 {
  249.                     // Extract the bits
  250.                     list($type$data) = $theFilter;
  251.                     // Deal with in, not_in
  252.                     if(in_array($type, array("in""not_in")))
  253.                     {
  254.                         // What type
  255.                         if($type == "in")
  256.                         {
  257.                             $queryBuilder->andWhere($this->alias '.' $key ' IN(:' $key ')')
  258.                                 ->setParameter($key$data);
  259.                         }
  260.                         elseif($type == "not_in")
  261.                         {
  262.                             $queryBuilder->andWhere($this->alias '.' $key ' NOT IN(:' $key ')')
  263.                                 ->setParameter($key$data);
  264.                         }
  265.                     }
  266.                     // Deal with nulls
  267.                     elseif(in_array($type, array("is_null""not_null")))
  268.                     {
  269.                         // What type
  270.                         if($type == "is_null" && is_bool($data))
  271.                         {
  272.                             if($data)
  273.                                 $queryBuilder->andWhere($this->alias '.' $key ' IS NULL');
  274.                             else
  275.                                 $queryBuilder->andWhere($this->alias '.' $key ' IS NOT NULL');
  276.                         }
  277.                         elseif($type == "not_null" && is_bool($data))
  278.                         {
  279.                             if($data)
  280.                                 $queryBuilder->andWhere($this->alias '.' $key ' IS NOT NULL');
  281.                             else
  282.                                 $queryBuilder->andWhere($this->alias '.' $key ' IS NULL');
  283.                         }
  284.                         else
  285.                             throw new \Exception("Invalid query paramater 'null' value(s)");
  286.                     }
  287.                     // Deal with has all
  288.                     elseif(in_array($type, array("has_all")))
  289.                     {
  290.                         // Make sure we got some
  291.                         if(count($data))
  292.                         {
  293.                             // Loop them
  294.                             foreach($data AS $itemKey => $someItem)
  295.                             {
  296.                                 $queryBuilder->andWhere(':' $key $itemKey ' MEMBER OF ' $this->alias '.' $key)
  297.                                     ->setParameter($key $itemKey$someItem);
  298.                             }
  299.                         }
  300.                             
  301.                     }
  302.                     else
  303.                     {
  304.                         throw new \Exception("Invalid query paramater of type '" $type "' on field '" $key "'. Available options: in, not_in, lt, lte, gt, gte, is_null, not_null, has_all");
  305.                     }
  306.                 }
  307.             }
  308.         }
  309.         return $queryBuilder;
  310.     }
  311. }