0

I searched the internet and found a few solutions, but none of them worked in ORM Doctrine, it was a bug.

I want to achieve the effect as below.

Result:

enter image description here

Table schema:

enter image description here

1 Answers1

0

There are two ways to achieve the desired effect, and neither of them is available in standard DQL with the schema provided.

First, you might choose to use a recursive SQL query to get the proper ordering in each subtree. You'll need to map the results of the native query to be able to work on ORM objects again.

The other way is to add an extra column to the table, and store the full path (or any global positioning information) of each node. After this, a quick ORDER BY works flawlessly even in DQL. You might find the tree behavior extension handy to automate most parts.

pestaa
  • 4,749
  • 2
  • 23
  • 32
  • Examples of all kinds are on the linked page: https://github.com/Atlantic18/DoctrineExtensions/blob/v2.4.x/doc/tree.md#entity-mapping – pestaa Dec 03 '19 at 09:54
  • I recommend `materializedPath` strategy when starting out, and `nested` when you have more advanced feature requirements (selecting subtrees + high performance, for example). – pestaa Dec 03 '19 at 09:56