Error when use custom DQL function with Doctrine and Symfony2

前端 未结 2 1678
萌比男神i
萌比男神i 2021-02-03 10:36

I use Symfony 2 and the ORM Doctrine. I want to create and register a custom DQL function. In fact, I want to use the SQL function \"CAST\" in my request, like this :

         


        
2条回答
  •  Happy的楠姐
    2021-02-03 11:20

    Can't find the reference but functions are not allowed in the order by clause. You need to cast your value in the select statement then sort by it.

    Something like:

    $qb->select('d, CAST(d.myField AS UNSIGNED) AS sortx)
       ->from('\Test\MyBundle\Entity\MyEntity', 'd')
       ->orderBy('sortx, 'ASC')
    

    That is assuming your CAST function is written correctly.

提交回复
热议问题