Returns the orderBy statement for database connection

@param string $orderBy
@param string $tablename

@return string
558030
public static function getOrderByString($orderBy, $tablename)
    {
        if (isset($orderBy) && !empty($orderBy)) {
            $cleanOrderByParts = [];
            $tableColumns = $GLOBALS['TCA'][$tablename]['columns'];
            $orderByParts = GeneralUtility::trimExplode(',', $orderBy);
            foreach ($orderByParts as $part) {
                $subparts = GeneralUtility::trimExplode(' ', $part);
                if (count($subparts) === 1) {
                    if (is_array($tableColumns[$subparts[0]])) {
                        $cleanOrderByParts[] = $subparts[0];
                    }
                } elseif (count($subparts) === 2) {
                    if (is_array($tableColumns[$subparts[0]]) && ($subparts[1] === 'ASC' || $subparts[1] === 'DESC')) {
                        $cleanOrderByParts[] = $subparts[0] . ' ' . $subparts[1];
                    }
                }
            }

            return implode(',', $cleanOrderByParts);
        }

        return '';
    }
108364
public function getTableSchemaId(string $tableName): string
    {
        $sql = sprintf('SHOW FULL COLUMNS FROM %s;', $this->quoter->quoteName($tableName));
        $rows = $this->db->query($sql)->fetchAll(PDO::FETCH_ASSOC);

        return sha1(json_encode($rows));
    }
960981
protected function order_sql(?string $fieldname = null, string $order = 'ASC', ?string $tablealias = null) {

        if (!$tablealias) {
            $tablealias = 'analysable';
        }

        if ($order != 'ASC' && $order != 'DESC') {
            throw new \coding_exception('The order can only be ASC or DESC');
        }

        $ordersql = ' ORDER BY (CASE WHEN aua.timeanalysed IS NULL THEN 0 ELSE aua.timeanalysed END) ASC';
        if ($fieldname) {
            $ordersql .= ', ' . $tablealias . '.' . $fieldname .' ' . $order;
        }

        return $ordersql;
    }
283683
public function sortByArrayKeys(array $orderBy)
    {
        $result = $this->target;

        if ($orderBy) {
            $ordered = [];

            foreach ($orderBy as $key) {
                if (array_key_exists($key, $result)) {
                    $ordered[$key] = $result[$key];
                    unset($result[$key]);
                }
            }

            $result = $ordered + $result;
        }

        return $result;
    }
951738
public static function getOrderByParts(OrderBy $orderBy): array
    {
        @trigger_error(sprintf('The use of "%s::getOrderByParts()" is deprecated since 2.3 and will be removed in 3.0. Use "%s::getParts()" directly instead.', __CLASS__, OrderBy::class), E_USER_DEPRECATED);

        return $orderBy->getParts();
    }
1070322
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);
    }
329329
public function ordered($orderBy = 'orderby', $orderDir = 'orderdir')
	{
		// Look for our request vars of interest
		$this->orderBy  = \Request::getCmd($orderBy, $this->getState('orderby', $this->orderBy));
		$this->orderDir = \Request::getCmd($orderDir, $this->getState('orderdir', $this->orderDir));

		$qualifiedOrderBy = $this->orderBy;

		// If we have a '.' we'll assume the prefix is a relationship name
		if (strpos($this->orderBy, '.') !== false)
		{
			list($relationship, $field) = explode('.', $this->orderBy);

			// We have to join to apply the order by clause
			$relationship     = $this->$relationship()->join();
			$qualifiedOrderBy = $relationship->getQualifiedFieldName($field);
		}

		// Apply order clause
		$this->order($qualifiedOrderBy, $this->orderDir);

		// Set state for future use
		$this->setState('orderby', $this->orderBy);
		$this->setState('orderdir', $this->orderDir);

		return $this;
	}
67863
final public function orderBy($orderby, $direction = '')
    {
        if (strtolower($direction) == 'random') {
            $orderby = ''; // Random results want or don't need a field name
            $direction = $this->_randomKeyword;
        } elseif (trim($direction) != '') {
            $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' ' . $direction : ' ASC';
        }


        if (strpos($orderby, ',') !== FALSE) {
            $temp = array();
            foreach (explode(',', $orderby) as $part) {
                $part = trim($part);
                if (!in_array($part, $this->arrayAliasedTables)) {
                    $part = $this->identifiers(trim($part));
                }
                $temp[] = $part;
            }

            $orderby = implode(', ', $temp);
        } else if ($direction != $this->_randomKeyword) {
            $orderby = $this->identifiers($orderby);
        }

        $orderbyStatement = $orderby . $direction;

        $this->arrayOrderBy[] = $orderbyStatement;
        if ($this->arrayCaching === TRUE) {
            $this->arrayCacheOrderBy[] = $orderbyStatement;
            $this->arrayCacheExists[] = 'orderby';
        }

        return $this;
    }
971796
public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
    {
        $this->preFindBy($criteria);

        $result =  $this->doFindBy($criteria, $orderBy, $limit, $offset);
        $result = $this->postFindBy($result);

        return $result;
    }
1360658
public function orderBy($key, $direction = self::ORDER_ASC, $tableName = null)
    {
        if (!$tableName) {
            $tableName = $this->getTableName();
        }
        $this->orderBy[] = ['key' => $key, 'direction' => $direction, 'tableName' => $tableName];
        return $this;
    }
1124966
protected function processOrderBy($orderby, SelectStatement $selectStmt) {
    $persistenceFacade = ObjectFactory::getInstance('persistenceFacade');
    if ($orderby) {
      $ok = false;

      // reset current order by
      $selectStmt->reset(SelectStatement::ORDER);

      foreach ($orderby as $curOrderBy) {
        $orderByParts = preg_split('/ /', $curOrderBy);
        $orderAttribute = $orderByParts[0];
        $orderDirection = sizeof($orderByParts) > 1 ? $orderByParts[1] : 'ASC';
        $orderType = null;
        $orderTypeMapper = null;
        if (strpos($orderAttribute, '.') > 0) {
          // the type is included in the attribute
          $orderAttributeParts = preg_split('/\./', $orderAttribute);
          $orderAttribute = array_pop($orderAttributeParts);
          $orderType = join('.', $orderAttributeParts);
          $orderTypeMapper = self::getMapper($orderType);
        }
        else {
          // check all involved types
          foreach (array_keys($this->involvedTypes) as $curType) {
            $mapper = self::getMapper($curType);
            if ($mapper->hasAttribute($orderAttribute)) {
              $orderTypeMapper = $mapper;
              break;
            }
          }
        }
        if ($orderTypeMapper) {
          $orderTableName = $orderTypeMapper->getRealTableName();
          // get table alias if defined and if type was not set explicitly
          if (!$orderType) {
            $aliasNames = $selectStmt->getAliasNames($orderTableName);
            $orderTableName = sizeof($aliasNames) > 0 ? $aliasNames[0] : $orderTableName;
          }
          $orderAttributeDesc = $orderTypeMapper->getAttribute($orderAttribute);
          $orderColumnName = $orderAttributeDesc->getColumn();

          if ($orderTableName) {
            $orderAttributeFinal = $orderTableName.'.'.$orderColumnName;
            $selectStmt->order([$orderAttributeFinal.' '.$orderDirection]);
            $ok = true;
          }
        }
      }
      if (!$ok) {
        throw new UnknownFieldException($orderAttribute, "The sort field name '".$orderAttribute."' is unknown");
      }
    }
    // add order columns to selected columns as required for distinct queries
    $order = $selectStmt->getRawState(SelectStatement::ORDER);
    if (sizeof($order) > 0) {
      $columnNames = array_keys($selectStmt->getRawState(SelectStatement::COLUMNS));
      foreach($order as $curOrderBy) {
        $orderByParts = preg_split('/ /', $curOrderBy);
        $orderAttribute = $orderByParts[0];
        $orderType = null;
        if (strpos($orderAttribute, '.') > 0) {
          // the type is included in the attribute
          $orderAttributeParts = preg_split('/\./', $orderAttribute);
          $orderAttribute = array_pop($orderAttributeParts);
          $orderType = join('.', $orderAttributeParts);
        }
        if (!in_array($orderAttribute, $columnNames)) {
          $queryType = $this->getQueryType();
          $joinName = ($orderType != $queryType && $orderType != $persistenceFacade->getSimpleType($queryType)) ? $orderType: null;
          $selectStmt->addColumns([$orderAttribute => $orderAttribute], $joinName);
          $columnNames[] = $orderAttribute;
        }
      }
    }
  }
1287187
public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
    {
        $persister = $this->entityManager->getEntityPersister($this->className);

        return $persister->loadAll($criteria, $orderBy, $limit, $offset);
    }
761341
public function get($fields, $where = null, $orderBy = null)
    {
        // 按SELECT进行查询
        $table = self::selectArray($fields, $where, $orderBy, 1);

        // 如果没有查到
        if (!$table or !count($table)) {
            return null;
        }

        // 取第一行
        $row = $table[0];

        // 取所有的键名
        $keys = array_keys($row);

        // 取第一列的值
        return $row[$keys[0]];
    }
24806
public function orderBy($orderBy)
    {
        if (is_string($orderBy)) {
            foreach (explode(',', $orderBy) as $item) {
                if (preg_match('#^\s*([\w\.]+)(\s+asc|\s+desc)?$#i', $item, $match) !== 1) {
                    throw new MisuseException(['unknown `:1` order by for `:2` collection', $orderBy, get_class($this->getSource())]);
                }
                $this->_order[$match[1]] = (!isset($match[2]) || strtoupper(ltrim($match[2])) === 'ASC') ? 1 : -1;
            }
        } else {
            /** @noinspection ForeachSourceInspection */
            foreach ($orderBy as $field => $value) {
                if ((is_int($value) && $value === SORT_ASC) || (is_string($value) && strtoupper($value) === 'ASC')) {
                    $this->_order[$field] = 1;
                } else {
                    $this->_order[$field] = -1;
                }
            }
        }

        return $this;
    }
158475
public function sortByArrayValues(array $orderBy)
    {
        if (!$orderBy) {
            return $this->target;
        }

        $result = [];

        foreach ($orderBy as $value) {
            if ($keys = array_keys($this->target, $value)) {
                foreach ($keys as $key) {
                    $result[$key] = $value;
                }
            }
        }

        return $result;
    }
125130
protected function getOrder()
    {
        /** @var EntityRepository $repo */
        $repo = $this->getRepository();
        $table = $repo->getEntityDescriptor()->getTableName();
        $pmk = $repo->getEntityDescriptor()->getPrimaryKey()->getField();
        return "{$table}.{$pmk} DESC";
    }
537942
protected function getOrderByString(): string
    {
        $statement = '';
        $orderBys  = $this->builder->getOrderBy();
        foreach ($orderBys as $orderBy) {
            $statement .= $orderBy['column'] . ' ' . $orderBy['order'] . ', ';
        }

        $statement = substr($statement, 0, -2);
        if (!empty($statement)) {
            $statement = 'ORDER BY ' . $statement;
        }

        return $statement;
    }