Merge is not working as expected #6108

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

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

Bug Report

Q A
BC Break no
Version 2.6

Summary

Merge is not working for parent and children relation. There is an parent object and its children mapped through @OneToMany. Whenever merge is called parent is being updated however new children are added instead of updating the record.

Current behavior

On merge parent (current) object is updated but new children are added for unmanaged object.

How to reproduce

I am getting the following json data from the client. JSON contains the parent and child details as below -

{
  id : 1,
  name : "Parent"
  children : [
      { id : 1, name : "A" },
	  { id : 2, name : "B" }
   ]
}

I am mapping these json data to Parent and Child Object.

Parent

class Parent{


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

     /**
     * @OneToMany(targetEntity="Child",cascade={"merge"}, mappedBy="parent" )
     */
    protected $children;

}

Child

class SalesOrderDetail extends BaseEntity {

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

    /**
     * @ManyToOne(targetEntity="parent")
     * @JoinColumn(name="parent_id")
     */
    protected $salesOrder;

}

Expected behavior

It should update Parent and Children if id is already available and add new record if id is not available.

Originally created by @sks40gb on GitHub (Nov 20, 2018). ### Bug Report <!-- Fill in the relevant information below to help triage your issue. --> | Q | A |------------ | ------ | BC Break | no | Version | 2.6 #### Summary Merge is not working for parent and children relation. There is an parent object and its children mapped through @OneToMany. Whenever `merge` is called parent is being updated however new children are added instead of updating the record. #### Current behavior On `merge` parent (current) object is updated but new children are added for unmanaged object. #### How to reproduce I am getting the following json data from the client. JSON contains the parent and child details as below - { id : 1, name : "Parent" children : [ { id : 1, name : "A" }, { id : 2, name : "B" } ] } I am mapping these json data to Parent and Child Object. ***Parent*** class Parent{ /** @Id @Column(type="integer",name="order_no") @GeneratedValue * */ protected $id; /** * @OneToMany(targetEntity="Child",cascade={"merge"}, mappedBy="parent" ) */ protected $children; } ***Child*** class SalesOrderDetail extends BaseEntity { /** @Id @Column(type="integer") @GeneratedValue * */ protected $id; /** * @ManyToOne(targetEntity="parent") * @JoinColumn(name="parent_id") */ protected $salesOrder; } #### Expected behavior It should update Parent and Children if `id` is already available and add new record if `id` is not available.
Author
Owner

@lcobucci commented on GitHub (Nov 20, 2018):

@sks40gb merge was removed from v3 and is being deprecate (with no replacement) on v2.7. The reason is because it doesn't support all edge cases and has an inconsistent behaviour.

The recommendation is to manipulate the objects instead of delegating that to the ORM.

@lcobucci commented on GitHub (Nov 20, 2018): @sks40gb merge was removed from v3 and is being deprecate (with no replacement) on v2.7. The reason is because it doesn't support all edge cases and has an inconsistent behaviour. The recommendation is to manipulate the objects instead of delegating that to the ORM.
Author
Owner

@sks40gb commented on GitHub (Nov 20, 2018):

@lcobucci - Thanks for your comment. This is really awkward, instead of fixing the inconsistent behaviour issue. The feature merge itself is being removed.

Merge is really very handy when it comes to updating the large object which has multiple children.

Now everything has to be done manually like adding, removing and updating the child object.

I have java hibernate background which works like a charm and I thought since Doctrine is inspired by it. it would be covering not all but important feature like merge.

Anyway do you have any reference how can we have handle this kind of scenario

@sks40gb commented on GitHub (Nov 20, 2018): @lcobucci - Thanks for your comment. This is really awkward, instead of fixing the inconsistent behaviour issue. The feature `merge` itself is being removed. Merge is really very handy when it comes to updating the large object which has multiple children. Now everything has to be done manually like adding, removing and updating the child object. I have java hibernate background which works like a charm and I thought since Doctrine is inspired by it. it would be covering not all but important feature like merge. Anyway do you have any reference how can we have handle this kind of scenario
Author
Owner

@Majkl578 commented on GitHub (Nov 20, 2018):

This is really awkward, instead of fixing the inconsistent behaviour issue. The feature merge itself is being removed.

Merge/detach stuff was surrounded with multiple inconsistent behaviours and gotchas, hence it's been removed.

Now everything has to be done manually like adding, removing and updating the child object.

There could be a merger tool, standalone to ORM, that would merge two entities just like EntityManager::merge() does.

@Majkl578 commented on GitHub (Nov 20, 2018): > This is really awkward, instead of fixing the inconsistent behaviour issue. The feature merge itself is being removed. Merge/detach stuff was surrounded with multiple inconsistent behaviours and gotchas, hence it's been removed. > Now everything has to be done manually like adding, removing and updating the child object. There could be a _merger_ tool, standalone to ORM, that would merge two entities just like `EntityManager::merge()` does.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6108