title: Doctrine: A new entity was found through the relationship
body: <p>I know, this is a famous problem, but I don't know how to solve it in my special case. I have the following class:</p>
  
  <pre><code>class Offer extends ModelEntity
  {
      /**
       * Primary Key.
       *
       * @var int
       *
       * @ORM\Id
       * @ORM\Column(name="id", type="integer", nullable=false)
       * @ORM\GeneratedValue(strategy="IDENTITY")
       */
      protected $id;
  
      /**
       * @var Provider
       *
       * @ORM\ManyToOne(targetEntity="Dashboard\Models\Provider")
       * @ORM\JoinColumn(name="provider_id", referencedColumnName="id", nullable=false)
       */
      protected $provider;
  
      /**
       * @var Article
       *
       * @ORM\OneToOne(targetEntity="Shopware\Models\Article\Article", cascade={"persist", "remove"})
       * @ORM\JoinColumn(name="article_id", referencedColumnName="id", nullable=false)
       */
      protected $article;
  
      /**
       * @var ArrayCollection
       *
       * @ORM\OneToMany(targetEntity="Dashboard\Models\Appointment", mappedBy="offer", cascade={"persist", "remove"})
       */
      protected $appointments;
  
      ...
  }
  </code></pre>
  
  <p>This is my current function to create an Offer object:</p>
  
  <pre><code>public function createOffer(array $data, Provider $provider)
  {
      // An article object is created here with persist and flush (Not my class and function, so I can't edit it)
      $article = $this-&gt;resource-&gt;create($data);
  
      $offer = new Offer();
      $offer-&gt;setProvider($provider);
      $offer-&gt;setArticle($article);
  
      $this-&gt;modelManager-&gt;merge($offer);
      $this-&gt;modelManager-&gt;flush();
      return $offer;
  }
  </code></pre>
  
  <p>I had to use the merge function, because when I used persist, I got the <strong>"A new entity was found through the relationship 'Dashboard\Models\Offer#provider' ..."</strong> error. With merge it is working. But now I want to create appointments for the offer. I tried it like this:</p>
  
  <pre><code>...
  $offer = new Offer();
  $offer-&gt;setProvider($provider);
  $offer-&gt;setArticle($article);
  
  if ($data["appointments"]) {
      foreach ($data["appointments"]["dates"] as $i =&gt; $date) {
          $appointment = new Appointment();
          $appointment-&gt;setDate(new \DateTime($date));
          $appointment-&gt;setStreet($data["appointments"]["streets"][$i]);
          $appointment-&gt;setZipcode($data["appointments"]["zipcodes"][$i]);
          $appointment-&gt;setCity($data["appointments"]["cities"][$i]);
          $appointment-&gt;setOffer($offer);
          $this-&gt;modelManager-&gt;persist($appointment);
      }
  }
  
  $this-&gt;modelManager-&gt;merge($offer);
  $this-&gt;modelManager-&gt;flush();
  ...
  </code></pre>
  
  <p>But with this I'm getting <strong>"A new entity was found through the relationship 'Dashboard\Models\Appointment#offer'..."</strong> And I also tried it with merge and with collecting all appointments in an ArrayCollection and then using $offer->setAppointments($appointments), but that was neither working. Can someone help me with it? What is the correct way? At the end here is the Appointment class:</p>
  
  <pre><code>class Appointment extends ModelEntity
  {
      /**
       * Primary Key.
       *
       * @var int
       *
       * @ORM\Id
       * @ORM\Column(name="id", type="integer", nullable=false)
       * @ORM\GeneratedValue(strategy="IDENTITY")
       */
      protected $id;
  
      /**
       * @var Offer
       *
       * @ORM\ManyToOne(targetEntity="Dashboard\Models\Offer", inversedBy="appointments")
       * @ORM\JoinColumn(name="offer_id", referencedColumnName="id", nullable=false)
       */
      protected $offer;
      ...
  } 
  </code></pre>