The TopologicalSort reverts the order of persisted entities #7244

Closed
opened 2026-01-22 15:47:47 +01:00 by admin · 4 comments
Owner

Originally created by @netbull on GitHub (Nov 11, 2023).

Bug Report

This was reported by #10897 and #10864, but the order is reversed when the nodes are also edges.

Q A
BC Break yes
Version 2.16.0

Summary

Use case:
Entity Booking and Entity Guest.
If we persist 3 Guests to existing Booking then we have the correct order of the guests as here its used array_reverse because here array_unshift is used.

Current behavior

if we persist new Booking with the same collection of Guests the collection is reversed, because the Guest nodes are also edges to Entity Booking.

How to reproduce

Persist 3 Guest Entities A, B and C and the order will be kept.
Persist new Booking Entity with 3 Guest Entities and the order of the guests will be C, B and A

Expected behavior

The edges array should be also reversed while iterating, the same way as the nodes.

Originally created by @netbull on GitHub (Nov 11, 2023). ### Bug Report This was reported by #10897 and #10864, but the order is reversed when the nodes are also edges. | Q | A |------------ | ------ | BC Break | yes | Version | 2.16.0 #### Summary Use case: Entity Booking and Entity Guest. If we persist 3 Guests to existing Booking then we have the correct order of the guests as [here](https://github.com/doctrine/orm/blob/2.16.x/lib/Doctrine/ORM/Internal/TopologicalSort.php#L107C23-L107C23) its used `array_reverse` because [here](https://github.com/doctrine/orm/blob/2.16.x/lib/Doctrine/ORM/Internal/TopologicalSort.php#L163) `array_unshift` is used. #### Current behavior if we persist new Booking with the same collection of Guests the collection is reversed, because the Guest nodes are also edges to Entity Booking. #### How to reproduce Persist 3 Guest Entities A, B and C and the order will be kept. Persist new Booking Entity with 3 Guest Entities and the order of the guests will be C, B and A #### Expected behavior The edges array should be also reversed while iterating, the same way as the nodes.
admin added the Bug label 2026-01-22 15:47:47 +01:00
admin closed this issue 2026-01-22 15:47:47 +01:00
Author
Owner

@derrabus commented on GitHub (Nov 11, 2023):

We don't guarantee any order in which entities are being persited. Please elaborate why you believe the current behavior to be defective.

@derrabus commented on GitHub (Nov 11, 2023): We don't guarantee any order in which entities are being persited. Please elaborate why you believe the current behavior to be defective.
Author
Owner

@netbull commented on GitHub (Nov 11, 2023):

@derrabus this says it all basically.

If we try to keep the order the same when the entities are added as nodes, then we should try keeping the order also if they are also edges.

@netbull commented on GitHub (Nov 11, 2023): @derrabus [this](https://github.com/doctrine/orm/blob/2.16.x/lib/Doctrine/ORM/Internal/TopologicalSort.php#L103C1-L104C1) says it all basically. If we try to keep the order the same when the entities are added as nodes, then we should try keeping the order also if they are also edges.
Author
Owner

@mpdude commented on GitHub (Nov 12, 2023):

#11058 has more elaborate discussion on the potential requirements or expectations

@mpdude commented on GitHub (Nov 12, 2023): #11058 has more elaborate discussion on the potential requirements or expectations
Author
Owner

@derrabus commented on GitHub (Nov 14, 2023):

Let's close this issue then. The behavior described here is not a bug.

We can continue to discuss on #11058 whether we want to change the current behavior or not.

@derrabus commented on GitHub (Nov 14, 2023): Let's close this issue then. The behavior described here is not a bug. We can continue to discuss on #11058 whether we want to change the current behavior or not.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#7244