title: Doctrine2 Entity PrePersist - Update another entity
body: <p>I have a Doctrine2 entity (call it entity A) in my Symfony2 project. This entity has a ManyToOne relationship with another entity (call it entity B) in the project.</p>
  
  <p>Entity A has a status property which is 'active' or 'inactive'. There's only one 'active' entity A allowed in entity B. So, if a new entity A is added to an existing entity B, the previous entity A, which has an 'active' status, needs to be updated to 'inactive'.</p>
  
  <p>What is the best approach to achieve this?</p>
  
  <p>I was thinking about the LifeCycle methods (prePersist), but I doubt if this works, because it's another entity which is updated than the entity I persist.</p>
  
  <p>A code example:</p>
  
  <pre><code>class EntityA
  {
      const ACTIVE = 'active';
      const INACTIVE = 'inactive';
  
      private $id;
      private $status;
      private $entityB;
  
      public function prePersist()
      {
          $currentEntityA = $this-&gt;entityB-&gt;getCurrentEntityA();
          if ($currentEntityA) {
              $currentEntityA-&gt;setStatus(self::INACTIVE);
          }
      }
  }
  
  class EntityB
  {
      private $id;
      private $name;
      private $entityA;
  
      public function getCurrentEntityA()
      {
          foreach($this-&gt;entityA as $row){
              if ($row-&gt;getStatus() == EntityA::ACTIVE ) {
                  return $row;
              }
          }
          //no entityA found so return null
          return null;
      }
  }
  </code></pre>