Merge is creating not working for children @OneToMany #6104

Open
opened 2026-01-22 15:26:50 +01:00 by admin · 0 comments
Owner

Originally created by @sks40gb on GitHub (Nov 17, 2018).

Q A
Version x.y.z

Merge is creating not working for children @OneToMany

I am using Php Doctrine and I am using @OnToMany mapping with cascade all. I have a parent class SalesOrder and child class SalesOrderDetails.

  1. Case 1 : Save - When I save new record sales order along with sales order details. It is working as expected.
  2. Case 2 : Update - Here is the issue, I am merging the Sales Order which is fine however its inserting new records for its children SalesOrderDetail instead of updating it. Ideally it should it apply merge but for children as well but its not.

As of now, I am getting the Sales Order Details by id from DB then change the properties of it. Ideally that should not be the case, mean if we set the id to unmanned object, it should update instead of creating new records.

Note:

  1. Merge is working with parent object if it has the id value.
  2. I am not adding new item here, I am just updating the existing recorded through merge.

SalesOrder.php

/**
 * @Entity @Table(name="sales_orders")
 * */
class SalesOrder extends BaseEntity {
       /**
       * @OneToMany(targetEntity="SalesOrderDetail",cascade="all", mappedBy="salesOrder" )
       */
       protected $itemSet;
       
           function __construct() {
                $this->itemSet = new ArrayCollection();
          }
}

SalesOrderDetail.php

/**
 * @Entity @Table(name="sales_order_details")
 * */
class SalesOrderDetail extends BaseEntity {

    /** @Id @Column(type="integer") @GeneratedValue * */
    protected $id;

    /**
     * @ManyToOne(targetEntity="SalesOrder")
     * @JoinColumn(name="order_no", referencedColumnName="order_no")
     */
    protected $salesOrder;

}
Originally created by @sks40gb on GitHub (Nov 17, 2018). <!-- Fill in the relevant information below to help triage your issue. --> | Q | A |------------ | ----- | Version | x.y.z <!-- Before asking question here, please try asking on Gitter or Slack first. Find out more about Doctrine support channels here: https://www.doctrine-project.org/community/ Keep in mind that GitHub is primarily an issue tracker. --> ### Merge is creating not working for children @OneToMany I am using Php Doctrine and I am using `@OnToMany` mapping with `cascade` `all`. I have a parent class `SalesOrder` and child class `SalesOrderDetails`. 1. `Case 1 : Save` - When I save new record sales order along with sales order details. It is working as expected. 2. `Case 2 : Update` - Here is the issue, I am merging the `Sales Order` which is fine however its inserting new records for its children `SalesOrderDetail` instead of updating it. Ideally it should it apply `merge `but for children as well but its not. As of now, I am getting the Sales Order Details by id from DB then change the properties of it. Ideally that should not be the case, mean if we set the id to unmanned object, it should update instead of creating new records. **Note:** 1. Merge is working with parent object if it has the id value. 2. I am not adding new item here, I am just updating the existing recorded through merge. **SalesOrder.php** /** * @Entity @Table(name="sales_orders") * */ class SalesOrder extends BaseEntity { /** * @OneToMany(targetEntity="SalesOrderDetail",cascade="all", mappedBy="salesOrder" ) */ protected $itemSet; function __construct() { $this->itemSet = new ArrayCollection(); } } **SalesOrderDetail.php** /** * @Entity @Table(name="sales_order_details") * */ class SalesOrderDetail extends BaseEntity { /** @Id @Column(type="integer") @GeneratedValue * */ protected $id; /** * @ManyToOne(targetEntity="SalesOrder") * @JoinColumn(name="order_no", referencedColumnName="order_no") */ protected $salesOrder; }
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6104