Search code examples
phpdoctrine-ormsql-order-byversiondql

Extending my ORDER BY in Doctrine2


I have a table that holds version numbers and I want to read them out naturally sorted.
This is no problem with raw SQL:

SELECT * FROM versions ORDER BY REPLACE(version, '.', '')+0 DESC

But if I write my query in Doctrine2 like this:

$qry = $this->createQueryBuilder('v')
  ->select('v')
  ->orderBy("REPLACE(v.version, '.', '')+0", 'DESC');

I only get

Fatal error: Uncaught exception 'Doctrine\ORM\Query\QueryException' with message '[Syntax Error] line 0, col 100: Error: Expected end of string, got '('' in .......

Does anybody know how to write this query for Doctrine2?


Solution

  • After long time struggeling, I realized that I needed to write out the query using NativeQuery:

    $rsm = new \Doctrine\ORM\Query\ResultSetMapping();
    // do some result mapping here ....
    
    $query = $this->_em->createNativeQuery("SELECT v.* FROM versions v ORDER BY REPLACE(v.version, '.', '')+0 DESC", $rsm);
    
    $versions = $query->getResult();