Example #1
0
 /**
  * processing supllying custom query made
  * @param array $options
  * @return list of object 
  */
 public static function processQuery($options = array())
 {
     $className = self::alias();
     $qb = Model::getEntity()->createQueryBuilder();
     $selected = array();
     if (isset($options['where'])) {
         if (isset($options['where']['and'])) {
             $and = $qb->expr()->andx();
             foreach ($options['where']['and'] as $rule) {
                 $and->add(self::addRule($qb, $rule));
             }
         }
         if (isset($options['where']['or'])) {
             $or = $qb->expr()->orx();
             foreach ($options['where']['or'] as $rule) {
                 $or->add(self::addRule($qb, $rule));
             }
         }
     }
     if (isset($options['leftJoin'])) {
         foreach ($options['leftJoin'] as $join) {
             $selected[] = self::alias($join['field']);
         }
     }
     if (isset($options['innerJoin'])) {
         foreach ($options['innerJoin'] as $join) {
             $selected[] = self::alias($join['field']);
         }
     }
     $qb->select($className . (!empty($selected) ? ", " : "") . implode(", ", $selected) . (isset($options['groupBy']['select']) ? ", {$options['groupBy']['select']}" : ""))->from(get_called_class(), $className);
     $and = self::addJoin($qb, $options, $className, isset($and) ? $and : null);
     if (isset($and)) {
         $qb->where($and);
     }
     if (isset($or)) {
         $qb->orWhere($or);
     }
     if (isset($options['orderBy'])) {
         $orders = array();
         foreach ($options['orderBy']['fields'] as $field) {
             $orders[] = "{$className}.{$field}";
         }
         $qb->addOrderBy(implode(",", $orders), isset($options['orderBy']['direction']) ? $options['orderBy']['direction'] : null);
     }
     if (isset($options['offset'])) {
         $qb->setFirstResult($options['offset']);
     }
     if (isset($options['limit'])) {
         $qb->setMaxResults($options['limit']);
     }
     if (isset($options['groupBy'])) {
         foreach ($options['groupBy']['fields'] as $by) {
             $qb->addGroupBy($by);
         }
     }
     return $qb;
 }