38 $this->chain = array();
48 if (empty($this->chain) && !($element->
getValue() instanceof
Entity))
50 throw new SystemException(
'The first element of chain should be Entity only.');
53 $this->chain[] = $element;
54 $this->definition =
null;
55 $this->definitionParts =
null;
58 $this->last_element = $element;
67 $this->chain = array_merge(array($element), $this->chain);
68 $this->definition =
null;
69 $this->definitionParts =
null;
80 $elements =
$chain->getAllElements();
82 for ($i=count($elements)-1; $i>=0; $i--)
90 return $this->chain[0];
111 $this->chain = array_slice($this->chain, 0, -1);
112 $this->definition =
null;
113 $this->definitionParts =
null;
116 $this->last_element = end($this->chain);
122 $this->chain = array_slice($this->chain, 1);
123 $this->definition =
null;
124 $this->definitionParts =
null;
132 foreach ($this->chain as $element)
134 if ($element->isBackReference())
155 if (!isset($this->definition[$elementsSlice]))
157 $this->definition[$elementsSlice] = join(
'.',
164 return $this->definition[$elementsSlice];
172 if (is_null($this->definitionParts))
174 $this->definitionParts = static::getDefinitionPartsByChain($this);
182 if ($this->custom_alias !==
null)
187 if ($this->alias ===
null)
189 $this->alias = self::getAliasByChain($this);
197 $this->custom_alias =
$alias;
219 $def_elements_size = count($def_elements);
221 $prev_entity = $init_entity;
225 foreach ($def_elements as &$def_element)
227 $is_last_elem = (++$i == $def_elements_size);
234 if ($prev_entity->hasField($def_element))
237 $field = $prev_entity->
getField($def_element);
241 $prev_entity = $field->getRefEntity();
247 elseif ($field instanceof OneToMany)
249 $prev_entity = $field->getRefEntity();
251 elseif ($field instanceof ManyToMany)
253 $prev_entity = $field->getRefEntity();
255 elseif (!$is_last_elem)
257 throw new SystemException(sprintf(
258 'Normal fields can be only the last in chain, `%s` %s is not the last.',
259 $field->getName(), get_class($field)
263 if ($is_last_elem && $field instanceof ExpressionField)
266 $field = clone $field;
269 $chain->addElement(
new ChainElement($field));
278 $field = end($ref_entity->getReferencesTo($prev_entity->getName()));
280 $prev_entity = $ref_entity;
282 $chain->addElement(
new ChainElement(
283 array($ref_entity, $field)
286 elseif ( ($pos_wh = strpos($def_element,
':')) > 0 )
288 $ref_entity_name = substr($def_element, 0, $pos_wh);
290 if (strpos($ref_entity_name,
'\\') ===
false)
293 $ref_entity_name = $prev_entity->getNamespace().$ref_entity_name;
298 &&
Entity::getInstance($ref_entity_name)->hasField($ref_field_name = substr($def_element, $pos_wh + 1))
306 $reference->getRefEntity()->getFullName() == $prev_entity->getFullName() ||
308 $prev_entity->getDataClass(),
309 $reference->getRefEntity()->getDataClass()
316 $chain->addElement(
new ChainElement(array(
334 elseif ($def_element ==
'*' && $is_last_elem)
346 throw new SystemException(sprintf(
347 'Unknown field definition `%s` (%s) for %s Entity.',
348 $def_element,
$definition, $prev_entity->getFullName()
358 return join(
'.', static::getDefinitionPartsByChain(
$chain));
361 public static function getDefinitionPartsByChain(
Chain $chain)
367 $elements = array_slice(
$chain->getAllElements(), 1);
369 foreach ($elements as $element)
377 $def[] = $element->getDefinitionFragment();
385 if ($currentDefinition !==
'')
387 $currentDefinition .=
'.';
390 return $currentDefinition.$newDefinitionPart;
393 public static function getAliasByChain(
Chain $chain)
397 $elements =
$chain->getAllElements();
400 if (count($elements) > 2)
402 $alias[] =
$chain->getFirstElement()->getAliasFragment();
407 $elements = array_slice($elements, 1);
409 foreach ($elements as $element)
411 $fragment = $element->getAliasFragment();
432 return self::getChainByDefinition($entity,
$definition)->getAlias();
439 public function hasAggregation()
441 $elements = array_reverse($this->chain);
443 foreach ($elements as $element)
459 public function hasSubquery()
461 $elements = array_reverse($this->chain);
463 foreach ($elements as $element)
466 if ($element->getValue() instanceof ExpressionField && $element->getValue()->hasSubquery())
503 $helper = $this->
getLastElement()->getValue()->getEntity()->getConnection()->getSqlHelper();
504 $sql_def .=
' AS ' . $helper->quote($this->
getAlias());
512 $this->custom_alias =
null;
521 foreach ($this->chain as $elem)
static getInstance($entityName)
static appendDefinition($currentDefinition, $newDefinitionPart)
static getDefinitionByChain(Chain $chain)
prependElement(ChainElement $element)
addElement(ChainElement $element)
getSqlDefinition($with_alias=false)
static getAliasByDefinition(Entity $entity, $definition)
getDefinition($elementsSlice=0)