knp-components icon indicating copy to clipboard operation
knp-components copied to clipboard

OrderByWalker problem.

Open ctx2002 opened this issue 7 years ago • 1 comments

Knp\Component\Pager\Event\Subscriber\Sortable\Doctrine\ORM\Query\OrderByWalker produce wrong sql.

here is my DQL SELECT org FROM MyApp\Organisation org ORDER BY UPPER(TRIM(org.name)) ASC

$trim = $query->expr()->trim("org.name");
$exp = $query->expr()->upper($trim);

        $organisations = $paginator->paginate(
            $query->select('org')->orderby($exp,"ASC")->getQuery(),
            $request->query->get('page', 1),
            50
        );

After OrderByWalker modification, it produce a wrong sql, SELECT DISTINCT o0_.id AS id_0, o0_.name AS name_1 FROM organisation o0_ ORDER BY **o0_.name** DESC, UPPER(TRIM(o0_.name)) ASC LIMIT 50 OFFSET 0

the reason is, Doctrine AST parser produces following output.

OrderByClause {#1052 ▼
  +orderByItems: array:1 [▼
    0 => OrderByItem {#1051 ▼
      +expression: UpperFunction {#1059 ▼
        +stringPrimary: TrimFunction {#1055 ▼
          +leading: null
          +trailing: null
          +both: null
          +trimChar: false
          +stringPrimary: PathExpression {#1050 ▶}
          +name: "trim"
        }
        +name: "upper"
      }
      +type: "ASC"
    }
  ]
}

see above, order OrderByItem's expression is UpperFunction not PathExpression

in OrderByWalker, this leads to UpperFunction been removed from AST.

if (!$set) {
       array_unshift($AST->orderByClause->orderByItems, $orderByItem);
 }

i do not understand why need to remove orderByItem.

ctx2002 avatar Feb 01 '18 22:02 ctx2002

Current implementation supports order only for instances of Doctrine\ORM\Query\AST\PathExpression, while you're using an instace of Doctrine\ORM\Query\AST\Functions\FunctionNode See https://github.com/KnpLabs/knp-components/blob/master/src/Knp/Component/Pager/Event/Subscriber/Sortable/Doctrine/ORM/Query/OrderByWalker.php#L80

garak avatar Jun 24 '19 12:06 garak