title: Symfony2 - Doctrine Save entites with one to one relation
body: <p>I want to save a form that I have created but I have an error. </p>
  
  <p>I have 3 entities:</p>
  
  <pre><code>class T {
      /**
       * @ORM\OneToOne(targetEntity="MyNameSpace\ProfileBundle\Entity\Person", cascade={"persist"})
  
       */
      private $information;
  }
  
  class Person {
      /**
       * @ORM\OneToOne(targetEntity="MyNameSpace\MediaBundle\Entity\Document", cascade={"persist"}))
       */
      private $photo_profile;
  }
  
  class Document
  {
      private $file;
  }
  </code></pre>
  
  <p>When I save my  "T" class with this code :</p>
  
  <pre><code>public function createAction()
  {
          $entity     = new T();                        
          $request = $this-&gt;getRequest();
          $form    = $this-&gt;createForm(new TType(), $entity);        
          $form-&gt;bindRequest($request);
  
          if ($form-&gt;isValid()) 
          {
              $em = $this-&gt;getDoctrine()-&gt;getEntityManager();
              $em-&gt;persist($entity);            
              $em-&gt;flush();
          }
  } 
  </code></pre>
  
  <p>I have this error:</p>
  
  <blockquote>
    <p>SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (<code>namespace</code>.<code>person</code>, CONSTRAINT <code>FK_3370D440809EFCB0</code> FOREIGN KEY (<code>photo_profile_id</code>) REFERENCES <code>Document</code> (<code>id</code>)) </p>
  </blockquote>
  
  <p>Any helps will be cool</p>
  
  <p>Thank you for all</p>
  
  <hr>
  
  <p>Here the code I have:</p>
  
  <pre><code>/**
   * Creates a new Tutor entity.
   */
  public function createAction()
  {       
      $entity  = new T();        
      $person = new Person();        
  
      $document = new Document();
      $person-&gt;setPhoto($document);
  
      $entity-&gt;setInformation($person);
  
      $request = $this-&gt;getRequest();
      $form    = $this-&gt;createForm(new TType(), $entity);        
      $form-&gt;bindRequest($request);
  
      if ($form-&gt;isValid()) {
          $em = $this-&gt;getDoctrine()-&gt;getEntityManager();
  
          $em-&gt;persist($entity-&gt;getInformation()-&gt;getPhoto());
          $em-&gt;persist($entity-&gt;getInformation());           
          $em-&gt;flush();
  
          $em-&gt;persist($entity);
          $em-&gt;flush();                       
  
          return $this-&gt;redirect($this-&gt;generateUrl('Index'));
      }
  </code></pre>
  
  <p>and I have this error:</p>
  
  <blockquote>
    <p>SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (<code>db</code>.<code>T</code>, CONSTRAINT <code>FK_58C6694C2EF03101</code> FOREIGN KEY (<code>information_id</code>) REFERENCES <code>Person</code> (<code>id</code>)) </p>
  </blockquote>
  
  <p>Any help please will be cool</p>
  
  <p>Thanks</p>