Example #1
0
 /**
  * @param \APY\DataGridBundle\Grid\Column\Column $column
  * @return string
  */
 protected function getFieldName($column, $withAlias = false)
 {
     $name = $column->getField();
     if ($column->getIsManualField()) {
         return $column->getField();
     }
     if (strpos($name, '.') !== false) {
         $previousParent = '';
         $elements = explode('.', $name);
         while ($element = array_shift($elements)) {
             if (count($elements) > 0) {
                 $parent = $previousParent == '' ? $this->getTableAlias() : $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 = $this->getTableAlias();
         $alias = $name;
     } else {
         return $this->getTableAlias() . '.' . $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;
 }