eZ\Publish\Core\Persistence\Doctrine\SelectDoctrineQuery::getQuery PHP Метод

getQuery() публичный Метод

Returns the query string for this query object.
public getQuery ( ) : string
Результат string
    public function getQuery()
    {
        if (count($this->parts['select']) === 0) {
            throw new QueryException('Missing "select" parts to generate query.');
        }
        $sql = 'SELECT ';
        if ($this->distinct) {
            $sql .= 'DISTINCT ';
        }
        $sql .= implode(', ', $this->parts['select']) . ' FROM';
        if (count($this->parts['from']) === 0) {
            throw new QueryException('Missing "from" parts to generate query.');
        }
        $renderedFromBefore = false;
        foreach ($this->parts['from'] as $fromPart) {
            if ($fromPart['type'] === 'FROM') {
                if ($renderedFromBefore === true) {
                    $sql .= ',';
                }
                $sql .= ' ' . $fromPart['table'];
                $renderedFromBefore = true;
            } else {
                $sql .= ' ' . $fromPart['type'] . ' JOIN ' . $fromPart['table'];
                if ($fromPart['condition']) {
                    $sql .= ' ON ' . $fromPart['condition'];
                }
            }
        }
        if (count($this->parts['where']) > 0) {
            $sql .= ' WHERE ' . implode(' AND ', $this->parts['where']);
        }
        if (count($this->parts['groupBy']) > 0) {
            $sql .= ' GROUP BY ' . implode(', ', $this->parts['groupBy']);
        }
        if (count($this->parts['having']) > 0) {
            $sql .= ' HAVING ' . implode(' AND ', $this->parts['having']);
        }
        if (count($this->parts['orderBy']) > 0) {
            $sql .= ' ORDER BY ' . implode(', ', $this->parts['orderBy']);
        }
        if ($this->limit || $this->offset) {
            $sql = $this->connection->getDatabasePlatform()->modifyLimitQuery($sql, $this->limit, $this->offset);
        }
        return $sql;
    }

Usage Example

 /**
  * Returns the SQL string for the subselect.
  *
  * Example:
  * <code>
  * <?php
  * $subSelect = $q->subSelect();
  * $subSelect->select( name )->from( 'table2' );
  * $q->select( '*' )
  *   ->from( 'table1' )
  *   ->where ( $q->expr->eq( 'name', $subSelect ) );
  * $stmt = $q->prepare();
  * $stmt->execute();
  * ?>
  * </code>
  *
  * @return string
  */
 public function getQuery()
 {
     return '( ' . parent::getQuery() . ' )';
 }