title: Symfony2 - Updating an entity in a one-to-one relationship
body: <p>I am trying to update my SingleTask entity however no change is accepted. The relationship between my Event and SingleTask is a bi-directional one-to-one relationship.</p>
  
  <p>The relevant portions of my Event entity are:</p>
  
  <pre><code>class Event
  {
     // properties
  
     /**
      * @OneToOne(targetEntity="SingleTask", mappedBy="event", cascade={"persist", "remove"})
      */
     private $singletask;
  
     public function setSingleTask(SingleTask $singletask)
     {
        $this-&gt;singletask = $singletask;
        $singletask-&gt;setEvent($this);
     }
  
     public function getSingleTask()
     {
        return $this-&gt;singletask;
     }
  }
  </code></pre>
  
  <p>The relevant portions of my SingleTask entity are:</p>
  
  <pre><code>/**
   * @Entity
   */
  class SingleTask
  {
     /**
      * @Column(type="datetime", nullable=true)
      */
     private $lastRun;
  
     /**
       * @OneToOne(targetEntity="Event", inversedBy="singletask")
       * @JoinColumn(name="event_id", referencedColumnName="id", nullable=true)
       **/
     private $event;
  
     public function setLastRun(\DateTime $lastRun = null)
     {
        $this-&gt;lastrun = $lastRun;
     }
  }
  </code></pre>
  
  <p>In a separate command file:</p>
  
  <pre><code>$eventRepo = $this-&gt;em-&gt;getRepository('HeyRemy\HeyRemyBundle\Entity\Event');
  $events = $eventRepo-&gt;findAll();
  
  foreach ($events as $event)
  {
     $singletask = $event-&gt;getSingleTask();
  
     if ($singletask != null)
     {
        $singletask-&gt;setLastRun(new \DateTime);
        $event-&gt;setSingleTask($singletask);
  
        $this-&gt;em-&gt;flush();
     }
  }
  </code></pre>
  
  <p>Unfortunately, the lastRun property on the SingleTask is never updated in the database. What is not correct?</p>
  
  <p>Edit: Code samples of how I create an event:</p>
  
  <pre><code>$event = new Event();
  $userRepo = $this-&gt;em-&gt;getRepository('HeyRemyBundle:User');
  $user = $userRepo-&gt;find($user_id);
  $event-&gt;setUser($user);
  
  $this-&gt;em-&gt;persist($event);
  $this-&gt;em-&gt;flush();
  
  $event_id = $event-&gt;getId();
  
  // other code which uses $event_id (SingleTask does not)
  
  $singletask = new SingleTask();
  $singletask-&gt;setNextRun($event_start);
  
  $this-&gt;em-&gt;persist($singletask);
  
  $event-&gt;setSingleTask($singletask);
  
  $this-&gt;em-&gt;flush();
  </code></pre>