Ejemplo n.º 1
0
 /**
  * @param \APY\DataGridBundle\Grid\Column\Column $column
  * @return string
  */
 protected function getFieldName($column, $withAlias = false)
 {
     $name = $column->getField();
     if ($column instanceof DQLColumn) {
         if (!$withAlias) {
             return $name;
         }
         return $column->getDql() . ' as ' . $name;
     } else {
         if (strpos($name, '.') !== false) {
             $previousParent = '';
             $elements = explode('.', $name);
             while ($element = array_shift($elements)) {
                 if (count($elements) > 0) {
                     $parent = $previousParent == '' ? self::TABLE_ALIAS : $previousParent;
                     $previousParent .= '_' . $element;
                     $this->joins[$previousParent] = array('field' => $parent . '.' . $element, 'type' => $column->getJoinType());
                 } else {
                     $name = $previousParent . '.' . $element;
                 }
             }
             $alias = str_replace('.', '::', $column->getId());
         } elseif (strpos($name, ':') !== false) {
             $previousParent = self::TABLE_ALIAS;
             $alias = $name;
         } else {
             return self::TABLE_ALIAS . '.' . $name;
         }
     }
     // Aggregate dql functions
     $matches = array();
     if ($column->hasDQLFunction($matches)) {
         if (strtolower($matches['parameters']) == 'distinct') {
             $functionWithParameters = $matches['function'] . '(DISTINCT ' . $previousParent . '.' . $matches['field'] . ')';
         } else {
             $parameters = '';
             if ($matches['parameters'] !== '') {
                 $parameters = ', ' . (is_numeric($matches['parameters']) ? $matches['parameters'] : "'" . $matches['parameters'] . "'");
             }
             $functionWithParameters = $matches['function'] . '(' . $previousParent . '.' . $matches['field'] . $parameters . ')';
         }
         if ($withAlias) {
             // Group by the primary field of the previous entity
             $this->query->addGroupBy($previousParent);
             $this->querySelectfromSource->addGroupBy($previousParent);
             return "{$functionWithParameters} as {$alias}";
         }
         return $alias;
     }
     if ($withAlias) {
         return "{$name} as {$alias}";
     }
     return $name;
 }