title: How to manage deserialized entities with entity manager?
body: <p>I am using JMSSerializerBundle to serialize my entities to json and deserialize json into entities, but I think this question applies for any deserialization techniques. </p>
  
  <p>For example, this schema:</p>
  
  <pre><code>class Order
  {
      private $id;
  
      /**
       * @Serializer\Type("ArrayCollection&lt;MyBundle\Entity\Order\Item&gt;")
       * @ORM\OneToMany(targetEntity="\MyBundle\Entity\Order\Item", mappedBy="order", cascade={"persist"})
       */
      private $items;
  }
  
  class Item
  {
  
      private $id;
  
      /**
       * @ORM\ManyToOne(targetEntity="\MyBundle\Entity\Order", inversedBy="items")
       */
      private $order;
  
      /**
       * @var integer $amount
       * @Serializer\Type("integer")
       * @ORM\Column(name="amount", type="integer")
       */
      private $amount;
  
  }
  </code></pre>
  
  <p>Maps to this json: <code>{"id":1,"items":[{"id":1,"amount":100}, {"id":2,"amount":200}]}</code> and the same json is properly deserialized into an object of type <strong>MyBundle:Order</strong> that has a colletion of two <strong>MyBundle:Order/Item</strong> objects. </p>
  
  <p>The problem is that when I try to persist this object, new entries are created in the database, rather than updating existing, ignoring the ids. How do I tell entity manager that theses objects should be updated, rather that created?</p>
  
  <p><strong>Update</strong>. Generally EntityManager::merge solution (as suggested by DaveM) is fine. But you must only merge existing objects. For example, if you have a json that represents a new Order entity that is connected to existing Order\Item entities </p>
  
  <blockquote>
    <p>{"id":null,"items":[{"id":1,"amount":100}, {"id":2,"amount":200}]}</p>
  </blockquote>
  
  <p>In this case you cannot just merge an Order object like this:
  <code>$em-&gt;merge($order)</code>, because order is a new entity and entity manager will attempt to find an Order object with id = null and you will end up with a new Order and empty items array. So the solution is to loop the Order::$items array and merge each item individually. Then a new order will be created and connected with existing items.</p>