Appends ORDER BY expression

@param string|array|\Krystal\Db\Sql\RawSqlFragmentInterface $type
@return \Krystal\Db\Sql\QueryBuilder
960439
public function orderBy($type = null)
    {
        if ($type === null) {
            $target = null;

        } elseif ($type instanceof RawSqlFragmentInterface) {
            $target = $type->getFragment();

        } elseif (is_array($type)) {
            // Special case for non-associative array
            if (!ArrayUtils::isAssoc($type)) {
                // Check if at least one value represents a raw fragment
                foreach ($type as &$option) {
                    // If raw, then do nothing but get it
                    if ($option instanceof RawSqlFragmentInterface) {
                        $option = $option->getFragment();
                    } else {
                        // Not raw - OK, then just quote it
                        $option = $this->quote($option);
                    }
                }
            } else {
                // If associative array supplied then assume that values represent sort orders
                $result = array();

                foreach ($type as $column => $sortOrder) {
                    // Force sorting method to be in upper case
                    $sortOrder = strtoupper($sortOrder);

                    // Ensure that only valid sorting methods provided
                    if (!in_array($sortOrder, array('ASC', 'DESC'))) {
                        throw new LogicException(sprintf(
                            'Only ASC and DESC methods are supported. You provided "%s"', $sortOrder
                        ));
                    }

                    // Only column names should be wrapped around backticks
                    array_push($result, sprintf('%s %s', $this->quote($column), $sortOrder));
                }

                $type = $result;
            }

            $target = implode(', ', $type);

        } else {
            $target = $this->quote($type);
        }

        $this->append(' ORDER BY '.$target);
        return $this;
    }
757211
public function findByType($type, $executeQuery = true)
    {
        $queryBuilder = $this->createQueryBuilder('w')
            ->andWhere($this->getWigetInstanceString($type))
        ;

        return $executeQuery ? $queryBuilder->getQuery()->getResult() : $queryBuilder->getQuery();
    }
927921
public function fetchAll($type = self::FETCH_TYPE_NUM)
    {
        if ($this->_allFetched) {
            return $this->buffer;
        }
        $results = $this->statement->fetchAll($type);
        if ($results !== false) {
            $this->buffer = array_merge($this->buffer, $results);
        }
        $this->_allFetched = true;
        $this->statement->closeCursor();

        return $this->buffer;
    }
493697
public function createForOrder(OrderInterface $order, $type = Invoice::TYPE_DEBIT)
    {
        $invoice = new Invoice();
        $number = $this->codeGenerator::generate($invoice);

        $data = [
            'order'        => $order,
            'number'       => $number,
            'date'         => date('d/m/Y'),
            'base_dir'     => $this->rootDir . '/../web',
            'invoice_type' => $type,
        ];

        $html = $this->templating->render($this->template, $data);
        $pdf = $this->pdfGenerator->getOutputFromHtml($html);

        $total = $type === Invoice::TYPE_DEBIT ?
            $order->getTotal() :
            $order->getTotal() * -1
        ;

        $order->addInvoice($invoice);
        $invoice->setOrder($order);
        $invoice->setNumber($number);
        $invoice->setMimeType('application/pdf');
        $invoice->setFile($pdf);
        $invoice->setType($type);
        $invoice->setTotal($total);

        return $invoice;
    }
203503
protected function applyQuerySort(QueryBuilder $queryBuilder, $alias = 'a')
    {
        $sort = $this->get('request')->get('sort');
        $order = $this->get('request')->get('order');

        if ($sort) {
            $repository = $this->get('orm.em')->getRepository($this->getEntityClass());
            // sort using the getQueryBuilderOrderedBy* magic method if defined
            $sortMethodName = 'getQueryBuilderOrderedBy'.ucfirst(strtolower($sort));
            if (method_exists($repository, $sortMethodName)) {
                $queryBuilder = call_user_method_array($sortMethodName, $repository, array($order, $queryBuilder));
            }
            // else sort by property name if exists
            elseif ($sort && property_exists($this->getEntityClass(), $sort)) {
                $queryBuilder->orderBy($alias.'.'.$sort, $order);
            }
        }
        // if empty sort use Gedmo Sortable annotation if enable
        elseif ($sort = $this->getSortableColumn()) {
            // pre order by group if defined
            if ($sortGroup = $this->getSortableGroup()) {
                $queryBuilder->orderBy($alias.'.'.$sortGroup);
            }
            $queryBuilder->addOrderBy($alias.'.'.$sort);
        }

        return $queryBuilder;
    }
708402
protected function doJoin($type, array $args)
    {
        if (count($args) === 0) {
            throw new QueryException('No arguments given');
        }

        $tableName = $args[0];
        $condition = '';

        if (count($args) == 2) {
            $condition = $args[1];
        } elseif (count($args) == 3) {
            $condition = $args[1] . ' = ' . $args[2];
        }

        $this->parts['from'][] = array(
            'table' => $tableName,
            'type' => $type,
            'condition' => $condition,
        );

        return $this;
    }
1162346
public function querySortHelper(BuilderInterface $query)
    {
        // process sort
        $rawSort = $this->searchHelper->getSort('sql');

        if ($rawSort != false) {
            $preparedSort = [];
            $sortFields = explode(',', $rawSort);

            foreach ($sortFields as $sortField) {
                // detect the correct name space for sort string
                // notice this might be a field name with a sort suffix
                $fieldBits = explode(' ', $sortField);

                // used to harmonize string after stripping out suffix
                $fieldName = $fieldBits[0];

                // assign a default value
                $suffix = '';
                if (count($fieldBits) > 1) {
                    // something like DESC/ASC
                    $suffix = $fieldBits[1];
                }

                // detect the correct table->namespace
                $prefix = $this->getTableNameSpace($fieldName);

                // now let's account for table:field entries
                $fieldBits = explode(':', $fieldName);
                $foo = count($fieldBits);
                if (count($fieldBits) > 1) {
                    $fieldName = $fieldBits[1];
                } else {
                    $fieldName = $fieldBits[0];
                }

                // put it all together for a working sort
                $preparedSort[] = ($prefix ? $prefix . '.' : '') . "[$fieldName]" . $suffix;
            }
            $query->orderBy(implode(',', $preparedSort));
        } else {
            // no sort requested, nothing to do here
            return $query;
        }
    }
161304
public function getStatement($type = null)
    {
        if (is_null($this->statement)) {
            $this->buffer = array();
            $this->next = false;

            $sql = "SELECT * FROM {$this->connection->getDialect()->grammarEscape($this->collection->getName())}";

            $wheres = array();
            $data = array();

            foreach ($this->criteria as $key => $value) {
                $wheres[] = $a= $this->connection->getDialect()->grammarExpression(
                    $key,
                    $value,
                    $this->collection,
                    $data
                );
            }

            if (!empty($wheres)) {
                $sql .= ' WHERE '.implode(' AND ', $wheres);
            }

            if (isset($this->sorts)) {
                $sorts = array();

                foreach ($this->sorts as $key => $value) {
                    if ($value == 1) {
                        $op = ' ASC';
                    } else {
                        $op = ' DESC';
                    }

                    $sorts[] = $this->connection->getDialect()->grammarEscape($key).$op;
                }

                if (!empty($sorts)) {
                    $sql .= ' ORDER BY '.implode(',', $sorts);
                }
            }

            if (isset($this->limit) || isset($this->skip)) {
                $sql .= ' LIMIT '.($this->limit ?: -1).' OFFSET '.($this->skip ?: 0);
            }

            
            $this->statement = $this->connection->getRaw()->prepare($sql);

            $this->statement->execute($data);
        }

        return $this->statement;
    }
1205129
public function orderBy() {
        $args = func_get_args();
        $this->build()->bindExpression('orderBy', $args[0] . " " . (empty($args[1]) ? ' ASC ' : " $args[1]"));

        return $this->connection;
    }
927782
public function orderRaw($raw)
    {
        if(is_array($raw))
        {
            foreach($raw as $k => $v)
            {
                if(is_numeric($k))
                {
                    $fieldName = $v;
                    $direction = 'asc';
                }
                else
                {
                    $fieldName = $k;
                    $direction = $v;
                }
                $this->option->order[] = new Order($fieldName, $direction);
            }
        }
        else
        {
            $order = new Order();
            $order->useRaw();
            $order->setRawSQL($raw);
            $this->option->order[] = $order;
        }
        return $this;
    }
701291
public function createQueryBuilder(array $criteria = null, array $orderBy = null, $limit = null, $offset = null, $class = null)
    {
        $baseQueryBuilder = $this->createBaseQueryBuilder($class);

        $queryBuilder = $this->alterBaseQueryBuilder($baseQueryBuilder);

        if (!is_null($criteria)) {
            foreach ($criteria as $key => $value) {
                $queryBuilder->andWhere($queryBuilder->getRootAliases()[0] . '.' . $key . ' = ' . $this->addNamedParameter($key, $value, $queryBuilder));
            }
        }

        if (!is_null($orderBy)) {
            foreach ($orderBy as $sort => $order) {
                $queryBuilder->addOrderBy($queryBuilder->getRootAliases()[0] . '.' . $sort, $order);
            }
        }

        if (!is_null($limit)) {
            $queryBuilder->setMaxResults($limit);
        }

        if (!is_null($offset)) {
            $queryBuilder->setFirstResult($offset);
        }

        return $queryBuilder;
    }
515545
public function index($type = null)
    {
        $this->_validateActionIsEnabled('index');

        $this->_event('beforeIndex');

        $this->paginate = [
            'limit' => 25,
            'order' => [
                ucfirst($this->Model->alias()) . '.id' => 'asc'
            ]
        ];

        foreach ($this->type['filters'] as $key => $value) {
            if (is_array($value)) {
                $this->Search->addFilter($key, $value);
            } else {
                $this->Search->addFilter($value);
            }
        }

        $query = $this->_callQuery($this->Model->find('all'));
        $query = $this->Search->search($query);

        $this->set('data', $this->paginate($query));

        $this->_event('afterIndex');
    }
1231048
public static function buildQuery(Array $data, String $separator = '/', String $type = 'none') : String
    {
        $query = NULL;

        foreach( $data as $key => $value )
        {
            if( is_numeric($key) )
            {
                $query .= $value . '/';
            }
            else
            {
                $query .= $key . '/' . $value . '/';
            }
        }

        return self::_addFix($query, $type);
    }
1036906
public function compileResequenceQuery(QueryBuilder $queryBuilder, ResequenceOrderIndexColumn $query) : \Dms\Core\Persistence\Db\Platform\CompiledQuery
    {
        $platform         = $queryBuilder->getConnection()->getDatabasePlatform();
        $outerTable       = $platform->quoteSingleIdentifier($query->getTable()->getName());
        $subSelectTable   = $platform->quoteSingleIdentifier($query->getTable()->getName() . '__inner');
        $orderIndexColumn = $platform->quoteSingleIdentifier($query->getColumn()->getName());

        $subSelect = $queryBuilder->getConnection()->createQueryBuilder();
        $subSelect
                ->from($outerTable, $outerTable . '__inner')
                ->select('COUNT(*) AS row_number')
                ->where($queryBuilder->expr()->lte(
                        $subSelectTable . '.' . $orderIndexColumn,
                        $outerTable . '.' . $orderIndexColumn
                ));

        $queryBuilder
                ->update($outerTable)
                ->set($orderIndexColumn, '(' . $subSelect->getSQL() . ')');


    }
615685
public function incrementColumn($type, $increment = 1)
    {
        if ($type === 'new') {
            return $this->incrementColumnInNew($increment);
        }

        return $this->incrementColumnInOld($increment);
    }
828429
public function create($type)
    {
        $typeOptions    = $this->getOption(self::OPTION_MAPPERS);
        $typeOption     = isset($typeOptions[$type]) ? $typeOptions[$type] : $this->throwException();
        $importerString = isset($typeOption[self::OPTION_MAPPERS_IMPORTER]) ? $typeOption[self::OPTION_MAPPERS_IMPORTER] : $this->throwException();
        $importer       = $this->buildService($importerString, ImportServiceInterface::class);

        if (isset($typeOption[self::OPTION_MAPPERS_MAPPER])) {
            $mapperString = $typeOption[self::OPTION_MAPPERS_MAPPER];
            $mapper       = $this->buildService($mapperString);
        } else {
            $mapper = $this->getDefaultMapper();
        }

        $this->propagate($mapper);
        $importer->setMapper($mapper);

        return $importer;
    }
456469
public function orderBy($fields, string $direction = 'ASC'): IQueryBuilderHandler
    {
        if (\is_array($fields) === false) {
            $fields = [$fields];
        }

        foreach ((array)$fields as $key => $value) {
            $field = $key;
            $type = $value;

            if (\is_int($key) === true) {
                $field = $value;
                $type = $direction;
            }

            if (($field instanceof Raw) === false) {
                $field = $this->addTablePrefix($field);
            }

            $this->statements['orderBys'][] = compact('field', 'type');
        }

        return $this;
    }