Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

orderBy array #74

Closed
qlarin opened this issue Apr 18, 2016 · 2 comments
Closed

orderBy array #74

qlarin opened this issue Apr 18, 2016 · 2 comments

Comments

@qlarin
Copy link

qlarin commented Apr 18, 2016

I came across a problem with select query. When i have tried to join two different tables to sort some results, i couldn't get correct sequence of 'order by' clause.
After analyzed, it seems that you split usual 'order by' from the one which is used in join. It is any chance to get correct sequence? I mean, i would like to get for example:

order by 'table1.col1', 'table2.col1', 'table1.col2',

but at the moment i have got something like:

order by 'table1.col1', 'table1.col2', 'table2.col1'.

@nilportugues
Copy link
Owner

Mind sharing the code used @qlarin ?

@qlarin
Copy link
Author

qlarin commented Apr 19, 2016

The code which is responsible for orders it's really simple. Something like that:

protected function querySort(Query $query, $sorts = null)
    {
        if (is_array($sorts)) {
            foreach ($sorts as $sort) {
                $order = (int)$sort['direction'] > 0 ? OrderBy::ASC : OrderBy::DESC;
                if (count($sort) == 5) {
                    $query->leftJoin(
                        $sort['table'],
                        $sort['joinBy'],
                        $sort['joinWith']
                    )->orderBy($sort['field'], $order);
                } else {
                    $query->orderBy($sort['field'], $order);
                }
            }
        }
        return $query;
    }

$query - for example:

SELECT DISTINCT 
 events.id AS "id", 
 events.date AS "date",
 events.phase_id AS "phase_id",
 events.league_id AS "league_id",
FROM events WHERE (events.league_id IN (:v1)) AND (events.date BETWEEN :v2 AND :v3) AND (events.league_id IS NOT NULL)

$sorts - this is associative array with 2 or 5 fields, if i wanna join another table to query i'm passing 3 extra fields, for example:

$sorts = [
                [
                    'field' => 'league_id',
                    'direction' => 1,
                ],
                [
                    'field' => 'phase_start_date',
                    'direction' => 1,
                    'table' => 'phases',
                    'joinBy' => 'phase_id',
                    'joinWith' => 'id',
                ],
                [
                    'field' => 'date',
                    'direction' => 1,
                ],
            ];

I quess problem is with getAllOrderBy() function in NilPortugues\Sql\QueryBuilder\Manipulation\Select

roelfsche added a commit to roelfsche/php-sql-query-builder that referenced this issue Jan 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants