OneToMany dependent property is being persisted in the reverse order #5497

Closed
opened 2026-01-22 15:09:10 +01:00 by admin · 2 comments
Owner

Originally created by @christopher-francisco on GitHub (Apr 11, 2017).

Originally assigned to: @Ocramius on GitHub.

I have an entity Order and Item as a 1:N (this is an example, I know in reality it'd be N:M).

I'm using the following method to add items to an order:

// Order.php
/* (...doctrine stuff...,  cascade={"persist"}) */
protected $items;

public function addItem(Item $item) {
    $item->setOrder($this);
    $this->items[] = $item;
}

// Item.php
public function setOrder(Order $order) {
   $this->order = $order;
}

// Somewhere else in the code
$order->addItem($item1);
$order->addItem($item2);
$order->addItem($item3);

$em->persist($order);
$em->flush();

The problem is the items are being persisted in the order $item3, $item2, $item1 rather than 1,2,3.

How can I solve this?

Originally created by @christopher-francisco on GitHub (Apr 11, 2017). Originally assigned to: @Ocramius on GitHub. I have an entity `Order` and `Item` as a `1:N` (this is an example, I know in reality it'd be N:M). I'm using the following method to add items to an order: ```php // Order.php /* (...doctrine stuff..., cascade={"persist"}) */ protected $items; public function addItem(Item $item) { $item->setOrder($this); $this->items[] = $item; } // Item.php public function setOrder(Order $order) { $this->order = $order; } // Somewhere else in the code $order->addItem($item1); $order->addItem($item2); $order->addItem($item3); $em->persist($order); $em->flush(); ``` The problem is the items are being persisted in the order `$item3, $item2, $item1` rather than `1,2,3`. How can I solve this?
admin added the Can't FixInvalid labels 2026-01-22 15:09:10 +01:00
admin closed this issue 2026-01-22 15:09:11 +01:00
Author
Owner

@Ocramius commented on GitHub (Apr 11, 2017):

You can't: there is no guarantee on persistence order here, as long as it
is valid. If you need an incremental sequence, then add one to your code,
and do not rely on auto-incremental identifiers here.

On 11 Apr 2017 4:16 p.m., "Christopher Francisco" notifications@github.com
wrote:

I have an entity Order and Item as a 1:N (this is an example, I know in
reality it'd be N:M).

I'm using the following method to add items to an order:

// Order.php/* (...doctrine stuff..., cascade={"persist"}) */protected $items;public function addItem(Item $item) { $item->setOrder($this); $this->items[] = $item;}// Item.phppublic function setOrder($order) { $this->order = $order;}// Somewhere else in the code$order->addItem($item1);$order->addItem($item2);$order->addItem($item3);$em->persist($order);$em->flush();

The problem is the items are being persisted in the order $item3, $item2,
$item1 rather than 1,2,3.

How can I solve this?


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/doctrine/doctrine2/issues/6391, or mute the thread
https://github.com/notifications/unsubscribe-auth/AAJakEwGi6-sXGdKJ5uUUedpzPAKOWTlks5ru4spgaJpZM4M6KSf
.

@Ocramius commented on GitHub (Apr 11, 2017): You can't: there is no guarantee on persistence order here, as long as it is valid. If you need an incremental sequence, then add one to your code, and do not rely on auto-incremental identifiers here. On 11 Apr 2017 4:16 p.m., "Christopher Francisco" <notifications@github.com> wrote: > I have an entity Order and Item as a 1:N (this is an example, I know in > reality it'd be N:M). > > I'm using the following method to add items to an order: > > // Order.php/* (...doctrine stuff..., cascade={"persist"}) */protected $items;public function addItem(Item $item) { $item->setOrder($this); $this->items[] = $item;}// Item.phppublic function setOrder($order) { $this->order = $order;}// Somewhere else in the code$order->addItem($item1);$order->addItem($item2);$order->addItem($item3);$em->persist($order);$em->flush(); > > The problem is the items are being persisted in the order $item3, $item2, > $item1 rather than 1,2,3. > > How can I solve this? > > — > You are receiving this because you are subscribed to this thread. > Reply to this email directly, view it on GitHub > <https://github.com/doctrine/doctrine2/issues/6391>, or mute the thread > <https://github.com/notifications/unsubscribe-auth/AAJakEwGi6-sXGdKJ5uUUedpzPAKOWTlks5ru4spgaJpZM4M6KSf> > . >
Author
Owner

@Ocramius commented on GitHub (Apr 11, 2017):

Closing as can't fix, invalid

@Ocramius commented on GitHub (Apr 11, 2017): Closing as `can't fix`, `invalid`
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5497