Began to study Doctrine 2 and translate sql queries for queries to EntityManager $this->em->getRepository(...)->...
But I can not translate a single request:
SELECT * FROM mytable ORDER BY (a - b - c) DESC LIMIT :offset, :rows
a, b, c - fields in the table mytable.
Everywhere there is only an example of how to sort by the difference of two numbers, but I cannot understand how to differentiate between the three.
How to remake this request for a request to EntityManager?

    1 answer 1

    In DQL, you do not work with database tables, but with entities, so instead of mytable, the query should contain the name of the entity class (perhaps in an abbreviated form), as well as its alias. This alias should be used as a prefix for the used entity attributes:

     SELECT entityAlias FROM NameSpace\EntityClassName entityAlias ORDER BY entityAlias.a - entityAlias.b - entityAlias.c 

    The LIMIT expression directly in DQL is not supported (perhaps because this construct is not standard for SQL and is implemented differently in different dialects). However, to set the offset and the number of sample results, you can use the setFirstResult () and setMaxResults () methods.

    Regarding the last part of the question:

    Everywhere there is only an example of how to sort by the difference of two numbers, but I cannot understand how to differentiate between the three.

    It does not matter how many fields are involved in the expression, 2, 3 or 100.

    • I understand about dql, everything is almost the same as in sql, except entities instead of tables. But I have all the queries in this form are $this->em->getRepository(...)->... and I didn’t want would depart from style. Can a given dql request be done somehow like $this->em->getRepository(...)->findBy(...)... ? - Vitaly Karpenko
    • Or is it preferable to do queries on dql? - Vitaly Karpenko
    • C findBy () is even simpler: - Timurib
    • findBy () accepts four parameters: selection criteria, sorting parameters, offset and limit elements: doctrine-project.org/api/orm/2.5/… - Timurib
    • Yes, I read. But I didn’t understand how to form a sorting array, if I just sort [полю id '=>' ASC '] by the field, but what should the array be in order for the difference of three fields? - Vitaly Karpenko