48 elseif (is_array($element)
49 && $element[0] instanceof
Entity
55 elseif ($element instanceof
Entity)
59 elseif ($element instanceof
Field)
65 throw new SystemException(sprintf(
'Invalid value for QueryChainElement: %s.', $element));
68 $this->value = $element;
87 if (array_key_exists($name, $this->parameters))
89 return $this->parameters[$name];
97 $this->parameters[$name] =
$value;
107 if (is_null($this->definition_fragment))
109 if ($this->type == 2)
112 if ($this->value->getRefEntity()->isUts())
114 $this->definition_fragment =
'';
118 $this->definition_fragment = $this->value->getName();
121 elseif ($this->type == 3)
124 if ($this->value[0]->isUtm())
126 $this->definition_fragment =
'';
130 $this->definition_fragment = $this->value[0]->getFullName() .
':' . $this->value[1]->getName();
133 elseif ($this->type == 4)
135 $this->definition_fragment =
'*';
139 if (!empty($this->parameters[
'uField']))
141 $this->definition_fragment = $this->parameters[
'uField']->getName();
145 $this->definition_fragment = $this->value->getName();
160 if (is_null($this->alias_fragment))
162 if ($this->type == 2)
165 if ($this->value->getRefEntity()->isUts())
167 $this->alias_fragment =
'';
171 $this->alias_fragment = $this->value->getName();
174 elseif ($this->type == 3)
177 if ($this->value[0]->isUtm())
179 $this->alias_fragment =
'';
183 $this->alias_fragment = $this->value[0]->getCode() .
'_' . $this->value[1]->getName();
186 elseif ($this->type == 4)
188 $this->alias_fragment = $this->value->getCode();
192 if (!empty($this->parameters[
'ufield']))
194 $this->alias_fragment = $this->parameters[
'ufield']->getName();
198 $this->alias_fragment = $this->value->getName();
211 public function getSqlDefinition()
213 if (is_array($this->value) || $this->value instanceof
Reference || $this->value instanceof
Entity)
216 'There is no SQL definition for Entity `%s`, please use a scalar field',
221 $helper = $this->value->getEntity()->getConnection()->getSqlHelper();
226 $buildFromChains = $this->value->getBuildFromChains();
228 foreach ($this->value->getBuildFrom() as $element)
230 if ($element instanceof \Closure)
233 $sqlExpression = $element();
238 'Expected instance of %s, got %s instead.',
239 SqlExpression::class, gettype($sqlExpression)
243 $SQLBuildFrom[] = $sqlExpression->compile();
247 $chain = array_shift($buildFromChains);
248 $SQLBuildFrom[] = $chain->getSQLDefinition();
252 $expr = $this->value->getExpression();
255 if (strpos($expr,
'%%TABLE_ALIAS') !==
false)
257 $expr = str_replace(
'%%TABLE_ALIAS', $helper->quote($this->getParameter(
'talias')), $expr);
261 $sql = call_user_func_array(
'sprintf', array_merge([$expr], $SQLBuildFrom));
265 $sql = $helper->quote($this->
getParameter(
'talias')) .
'.';
266 $sql .= $helper->quote($this->value->getColumnName());
279 if ($this->type === 3 || $this->value instanceof
OneToMany || $this->value instanceof
ManyToMany)
286 foreach ($this->value->getBuildFromChains() as $bfChain)
288 if ($bfChain->hasBackReference())
300 echo gettype($this->value).
' ';
302 if ($this->value instanceof
Field)
304 echo get_class($this->value).
' '.$this->value->getName();
306 elseif ($this->value instanceof
Entity)
308 echo get_class($this->value).
' '.$this->value->getFullName();
310 elseif (is_array($this->value))
312 echo
'('.get_class($this->value[0]).
', '.get_class($this->value[1]).
' '.$this->value[1]->getName().
')';
315 echo
' '.json_encode($this->parameters);
setParameter($name, $value)
__construct($element, $parameters=array())