/** * @return Fluent * @throws InvalidStateException */ public function createFluent() { if ($this->clauses->from === null or empty($this->clauses->select)) { throw new InvalidStateException(); } $statement = $this->connection->command(); foreach (array_keys($this->clauses->select) as $alias) { // SELECT $statement->select($this->queryHelper->formatSelect($this->domainQueryHelper->getReflection($this->aliases->getEntityClass($alias)), $alias)); if (array_key_exists($alias, $this->relationshipTables)) { call_user_func_array(array($statement, 'select'), array_merge(array('%n.%n AS %n, %n.%n AS %n, %n.%n AS %n'), $this->relationshipTables[$alias])); } } $statement->from(array($this->clauses->from['table'] => $this->clauses->from['alias'])); // FROM foreach ($this->clauses->join as $join) { // JOIN call_user_func_array(array($statement, $join['type']), array_merge(array('%n AS %n'), $join['joinParameters'])); call_user_func_array(array($statement, 'on'), array_merge(array('%n.%n = %n.%n'), $join['onParameters'])); } if (!empty($this->clauses->where)) { // WHERE call_user_func_array(array($statement, 'where'), $this->clauses->where); } foreach ($this->clauses->orderBy as $orderBy) { // ORDER BY $statement->orderBy('%n.%n', $orderBy[0], $orderBy[1]); if ($orderBy[2] === self::ORDER_DESC) { $statement->desc(); } } return $statement; }
/** * @param array $matches * @return string * @throws InvalidArgumentException */ private function translateMatches(array $matches) { if (!empty($matches[1])) { // quoted string return $matches[0]; } if (!empty($matches[2]) or !empty($matches[3])) { // modifier or placeholder if ($matches[2] !== 'else' and $matches[2] !== 'end') { $this->awaitedParameters++; } return $matches[0]; } $alias = $matches[4]; $property = $matches[5]; $entityClass = $this->aliases->getEntityClass($alias); $property = $this->getReflection($entityClass)->getEntityProperty($property); if ($property === null) { throw new InvalidArgumentException(); } $column = $property->getColumn(); if ($column === null) { throw new InvalidArgumentException(); } return "[{$alias}.{$column}]"; }