title: Doctrine ORM: Populate join column of related entities on persist
body: <p>Given the below simple entities in Doctrine ORM, is there a simple way to create a <code>Parent</code> object, add <code>Children</code> to it, and then persist the Parent and its children in one call to <code>EntityManager#persist()</code> and <code>EntityManager#flush()</code>?</p>
  
  <p>Currently, I'm persisting <code>Parent</code>, flushing the entity manager, then adding <code>Parent</code> to each <code>Child</code>, persisting each <code>Child</code> and then flushing the entity manager again. Otherwise, each <code>Child</code> is persisted to the database, but the join column is null. I tried a lifecycle callback on <code>Parent</code>, but <code>postPersist()</code> is called after all entities are persisted, not right after the parent entity and before the children are persisted.</p>
  
  <pre><code>class Parent {
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
  
    /**
     * @ORM\OneToMany(
     *    targetEntity="Child",
     *    mappedBy="parent",
     *    cascade={"persist", "merge", "remove"}
     * )
     */
    protected $children;
  
    public function __construct () {
      $this-&gt;children = new ArrayCollection;
    }
  
    public function addChild (Child $child) {
      $this-&gt;children[] = $child
    }
  }
  
  class Child {
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
  
    /**
     * @ORM\ManyToOne(
     *    targetEntity="Parent",
     *    inversedBy="children",
     * )
     */
    protected $parent;
  }
  </code></pre>