예제 #1
0
 /**
  * Gets the ORDER BY SQL snippet for ordered collections.
  *
  * @param array $orderBy
  * @param string $baseTableAlias
  * @return string
  */
 protected final function _getOrderBySQL(array $orderBy, $baseTableAlias)
 {
     $orderBySql = '';
     foreach ($orderBy as $fieldName => $orientation) {
         if (!isset($this->_class->fieldMappings[$fieldName])) {
             throw ORMException::unrecognizedField($fieldName);
         }
         $orientation = strtoupper(trim($orientation));
         if ($orientation != 'ASC' && $orientation != 'DESC') {
             throw ORMException::invalidOrientation($this->_class->name, $fieldName);
         }
         $tableAlias = isset($this->_class->fieldMappings[$fieldName]['inherited']) ? $this->_getSQLTableAlias($this->_class->fieldMappings[$fieldName]['inherited']) : $baseTableAlias;
         $columnName = $this->_class->getQuotedColumnName($fieldName, $this->_platform);
         $orderBySql .= $orderBySql ? ', ' : ' ORDER BY ';
         $orderBySql .= $tableAlias . '.' . $columnName . ' ' . $orientation;
     }
     return $orderBySql;
 }
예제 #2
0
    /**
     * Gets the ORDER BY SQL snippet for ordered collections.
     *
     * @param array  $orderBy
     * @param string $baseTableAlias
     *
     * @return string
     *
     * @throws \Doctrine\ORM\ORMException
     */
    protected final function getOrderBySQL(array $orderBy, $baseTableAlias)
    {
        $orderByList = array();

        foreach ($orderBy as $fieldName => $orientation) {

            $orientation = strtoupper(trim($orientation));

            if ($orientation != 'ASC' && $orientation != 'DESC') {
                throw ORMException::invalidOrientation($this->class->name, $fieldName);
            }

            if (isset($this->class->fieldMappings[$fieldName])) {
                $tableAlias = isset($this->class->fieldMappings[$fieldName]['inherited'])
                    ? $this->getSQLTableAlias($this->class->fieldMappings[$fieldName]['inherited'])
                    : $baseTableAlias;

                $columnName    = $this->quoteStrategy->getColumnName($fieldName, $this->class, $this->platform);
                $orderByList[] = $tableAlias . '.' . $columnName . ' ' . $orientation;

                continue;
            }

            if (isset($this->class->associationMappings[$fieldName])) {

                if ( ! $this->class->associationMappings[$fieldName]['isOwningSide']) {
                    throw ORMException::invalidFindByInverseAssociation($this->class->name, $fieldName);
                }

                $tableAlias = isset($this->class->associationMappings[$fieldName]['inherited'])
                    ? $this->getSQLTableAlias($this->class->associationMappings[$fieldName]['inherited'])
                    : $baseTableAlias;

                foreach ($this->class->associationMappings[$fieldName]['joinColumns'] as $joinColumn) {
                    $columnName    = $this->quoteStrategy->getJoinColumnName($joinColumn, $this->class, $this->platform);
                    $orderByList[] = $tableAlias . '.' . $columnName . ' ' . $orientation;
                }

                continue;
            }

            throw ORMException::unrecognizedField($fieldName);
        }

        return ' ORDER BY ' . implode(', ', $orderByList);
    }