Project

General

Profile

Actions

Bug #88876

closed

PageRepository ignores multiple orderings when fetching subpages for pages

Added by Elias Häußler over 5 years ago. Updated almost 5 years ago.

Status:
Closed
Priority:
Should have
Assignee:
-
Category:
Frontend
Target version:
-
Start date:
2019-08-01
Due date:
% Done:

100%

Estimated time:
TYPO3 Version:
9
PHP Version:
7.2
Tags:
Complexity:
easy
Is Regression:
Sprint Focus:

Description

The PageRepository currently does not allow to define multiple orderings when fetching subpages for pages.

When I call either $pageRepository->getMenu(<uid>, '*', 'crdate DESC, sorting ASC') or $pageRepository->getMenuForPages([<uids>], '*', 'crdate DESC, sorting ASC') (for example), the last defined order field (in this case sorting) takes precedence as it overrides all previously defined fields (in this case crdate).

Affected code:

if (!empty($sortField)) {
    $orderBy = QueryHelper::parseOrderBy($sortField);
    foreach ($orderBy as $order) {
        $res->orderBy(...$order);
    }
}

Source: https://github.com/TYPO3/TYPO3.CMS/blob/master/typo3/sysext/core/Classes/Domain/Repository/PageRepository.php#L760-L765

The problem is that each $res->orderBy() replaces the previous calls as the QueryBuilder replaces the queryPart instead of adding it.

I would suggest to check if an orderBy query part has been added previously and in this case to use $res->addOrderBy(). This leads to the following code:

if (!empty($sortField)) {
    $orderBy = QueryHelper::parseOrderBy($sortField);
    foreach ($orderBy as $order) {
        if (!$res->getQueryPart('orderBy')) {
            $res->orderBy(...$order);
        } else {
            $res->addOrderBy(...$order);
        }
    }
}

What do you think?

Actions #1

Updated by Jonas Eberle over 5 years ago

I think multiple orderBys are an undocumented feature so far.

/*
...
* @param string $sortField The field to sort by. Default is "sorting" 
...
*/
public function getMenuForPages(...

Is it used somewhere already with multiple ORDER BYs?

Actions #2

Updated by Elias Häußler over 5 years ago

Jonas Eberle wrote:

I think multiple orderBys are an undocumented feature so far.

[...]

That's true, it's currently not documented and it seems that is was meant to support only one sort field. But the implementation using the foreach loop shows that it might be a "feature" to support multiple fields. Also, the complexity should be pretty low to finally implement this.

Is it used somewhere already with multiple ORDER BYs?

It is currently not used with multiple sorting fields directly, but the AbstractMenuContentObject class offers some methods calling the getMenu() method of PageRepository where it could be possible to achieve this.

Actions #3

Updated by Gerrit Code Review over 5 years ago

  • Status changed from New to Under Review

Patch set 1 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/c/Packages/TYPO3.CMS/+/61425

Actions #4

Updated by Gerrit Code Review over 5 years ago

Patch set 1 for branch 9.5 of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/c/Packages/TYPO3.CMS/+/61371

Actions #5

Updated by Anonymous over 5 years ago

  • Status changed from Under Review to Resolved
  • % Done changed from 0 to 100
Actions #6

Updated by Gerrit Code Review over 5 years ago

  • Status changed from Resolved to Under Review

Patch set 2 for branch 9.5 of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/c/Packages/TYPO3.CMS/+/61371

Actions #7

Updated by Anonymous over 5 years ago

  • Status changed from Under Review to Resolved
Actions #8

Updated by Benni Mack almost 5 years ago

  • Status changed from Resolved to Closed
Actions

Also available in: Atom PDF