title: How to persist entities when one has two parents
body: <p>Here's my situation:</p>
  <p>Framework Symfony 4, using Doctrine(version).</p>
  <p>I have three entities : A, B and C.
  Entity A has a OneToMany relationship with B, and a OneToMany relationship with C.
  Entity B has a OneToMany relationship with C.</p>
  <p>In a form, I have a collectionForm about entity C embedded in a collectionForm about entity B.
  Adding entities B &amp; C is optional in the form.</p>
  <p>However, when trying to add entity C (and therefore entity B), seeing as entity C has two parents (A &amp; B), doctrine fails to know in which order to persist those, and gives the following error :</p>
  <blockquote>
  <p>A new entity was found through the relationship 'App\Entity\EntityC#entityB' that was not configured to cascade persist operations for entity: App\Entity\EntityB@0000000010cd7f460000000077359844. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={&quot;persist&quot;}). If you cannot find out which entity causes the problem implement 'App\Entity\EntityB#__toString()' to get a clue.</p>
  </blockquote>
  <p>How do I tell doctrine to persist entity A first, then B and then C in this case ?</p>
  <p>So far, I've tried removing the OneToMany relationship between B and C, without any luck, and adding a ManyToOne relationship in entityC for entity B.
  I feel if I could bypass this relationship between B and C this could work, but I don't know how.</p>
  <p>EntityA :</p>
  <pre class="lang-php prettyprint-override"><code>
  class entityA extends BaseEntityA {
      /**
       * @ORM\OneToMany(targetEntity=&quot;App\Entity\EntityB&quot;, mappedBy=&quot;entityA&quot;, cascade={&quot;persist&quot;})
       * @ORM\JoinColumn(name=&quot;id&quot;, referencedColumnName=&quot;entityA_id&quot;, nullable=false)
       */
      protected $entitiesB;
  
      /**
       * @ORM\OneToMany(targetEntity=&quot;App\Entity\EntityC&quot;, mappedBy=&quot;entityA&quot;, cascade={&quot;persist&quot;})
       * @ORM\JoinColumn(name=&quot;id&quot;, referencedColumnName=&quot;entityA_id&quot;, nullable=false)
       */
      protected $entitesC;
  
  ...
  
      /**
       * Add EntityB entity to collection (one to many).
       *
       * @param \App\Entity\EntityB $entityB
       *
       * @return \App\Entity\EntityA
       */
      public function addEntityB(\App\Entity\EntityB $entityB)
      {
          $this-&gt;entitiesB[] = $entityB;
          $entityB-&gt;setEntityA($this);
          return $this;
      }
  
      /**
       * Add EntityC entity to collection (one to many).
       *
       * @param \App\Entity\EntityC $entityC
       *
       * @return \App\Entity\EntityA
       */
      public function addEntityC(\App\Entity\EntityC $entityC)
      {
          $this-&gt;entitiesC[] = $entityC;
          $vehicle-&gt;setEntityA($this);
          return $this;
      }
  </code></pre>
  <p>Entity B :</p>
  <pre class="lang-php prettyprint-override"><code>
  class EntityB extends BaseEntityB {
      /**
       * @ORM\OneToMany(targetEntity=&quot;App\Entity\EntityC&quot;, mappedBy=&quot;entityB&quot;, cascade={&quot;persist&quot;})
       * @ORM\JoinColumn(name=&quot;id&quot;, referencedColumnName=&quot;entityB_id&quot;, nullable=false)
       */
      protected $entitiesC;
  
  ...
  
      /**
       * @param Collection $entitiesC
       *
       * @return $this
       */
      public function setEntityC(Collection $entitiesC)
      {
          $this-&gt;entitiesC = $entitiesC;
  
          return $this;
      }
  
      /**
       * Add EntityC entity to collection (one to many).
       *
       * @param \App\Entity\EntityC $entityC
       *
       * @return \App\Entity\EntityB
       */
      public function addEntityC(\App\Entity\EntityC $entityC)
      {
          $this-&gt;entitiesC[] = $entityC;
          $entityC-&gt;setEntityB($this);
          $entityC-&gt;setEntityA($this-&gt;getEntityA());
  
          // Things I tried to remove the relationship between B and C
  //        $this-&gt;getEntityA()-&gt;addEntityC($entityC);
  //        $entityC-&gt;setEntityB($this);
          return $this;
      }
  </code></pre>
  <p>Entity C :</p>
  <pre class="lang-php prettyprint-override"><code>
  class EntityC extends BaseEntityC {
  
  // Somtehing I tried at some point
  //    /**
  //     * @ORM\ManyToOne(targetEntity=&quot;EntityB&quot;, inversedBy=&quot;entitiesC&quot;, cascade={&quot;persist&quot;})
  //     * @ORM\JoinColumn(name=&quot;entityB_id&quot;, referencedColumnName=&quot;id&quot;)
  //     */
  //    protected $entityB;
  
  ...
  }
  </code></pre>
  <p>I would like to persist this entities in the right order (A then B then C), using only one form if possible.</p>