Doctrine2 Self Join, How to join yourself without a relationship column?

Mysql query example:

SELECT a1.* FROM agreement a1 LEFT JOIN agreement a2 on a1.agreementType = a2.agreementType and a2.id > a1.id WHERE a2.id is null 

The purpose of the request is to obtain the last consent of the return type. There are many types, and I only need a list of each last convention for each type. My sample query above works as expected, but does not go to DQL.

How can I do this in DQL, given that I don't have a column that refers to itself? Note that "agreementType" is also a foreign key for another table.

+4
source share
1 answer

Figured it out. I think I would share.

  SELECT a1 FROM My\Model\Agreement a1 LEFT JOIN My\Model\Agreement a2 WITH a1.agreementType = a2.agreementType AND a2.id > a1.id WHERE a2.id IS NULL 
+12
source

Source: https://habr.com/ru/post/1500434/


All Articles