title: Symfony2 Doctrine insert entity with related - NOT UPDATE
body: <p>I have problem with inserting User entity with related entity UserProfile.</p>
  
  <pre><code>class User
  {
  /**
   * @var integer
   *
   * @ORM\Id
   * @ORM\Column(name="id", type="integer",  options={"unsigned"=true})
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  protected $id;
  
  /**
   * @var UserProfile
   *
   * @ORM\OneToOne(targetEntity="UserProfile", mappedBy="user", cascade={"persist", "remove"})
   */
  protected $profile
  ....
  }
  
  
  class UserProfile
  {
  /**
   * @var User
   *
   * @ORM\Id
   * @ORM\GeneratedValue
   * @ORM\OneToOne(targetEntity="User",  inversedBy="profile" )
   * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
   */
  protected $user;
  .....
  }
  </code></pre>
  
  <p>Standard User-UserProfile bidirectional relation. When I submit form contains fields from User entity and UserProfile entity,  $form->getData() gives me User object with UserProfile. So far so good.</p>
  
  <p>Mysql generate a unique identity by auto-increment PK. To insert entity I have to do:</p>
  
  <pre><code>$em-&gt;persist($user)
  $em-&gt;flush() 
  </code></pre>
  
  <p>to get PK as id for persist and flush UserProfile.</p>
  
  <p>But doctrine CANT insert $user because $user object has related $user->profile received from form Type:</p>
  
  <pre><code>public function buildForm(FormBuilderInterface $builder, array $options)
  {
      $builder
       -&gt;add('email', 'email', array('label' =&gt; 'email'))
       -&gt;add('profile', new RegistrationProfileFormType(), array('required'=&gt;true,))
  }
  
  public function setDefaultOptions(OptionsResolverInterface $resolver)
  {
      $resolver-&gt;setDefaults(array(
          'data_class' =&gt; 'User',
          'cascade_validation'=&gt;true,
      ));
  }
  </code></pre>
  
  <p><strong>Update: Exception msg</strong> 
  Entity of type Entity\UserProfile has identity through a foreign entity Entity\User, however this entity has no identity itself. You have to call EntityManager#persist() on the related entity and make sure that an identifier was generated before trying to persist 'Entity\UserProfile'. In case of Post Insert ID Generation (such as MySQL Auto-Increment or PostgreSQL SERIAL) this means you have to call EntityManager#flush() between both persist operations.</p>
  
  <p>Q: How to handle this problem properly ?</p>
  
  <p>Sorry for my rusty English.</p>