title: Symfony Event Listener - populate entity
body: <p>Who can give example?
  I have the entities <code>Project</code> and <code>Event</code> - Many Event to one Project, so when status project changes to <code>'closed_by_client'</code>, I need to create an Event for this project. </p>
  
  <p>In the controller:</p>
  
  <pre><code>$project = $this
      -&gt;getDoctrine()
      -&gt;getManager()
      -&gt;getRepository('ArtelProfileBundle:Project')
      -&gt;find($id);
  
          $project-&gt;setCurrentStatus('closed_by_client');
          $manager-&gt;persist($project);
          $manager-&gt;flush();
  </code></pre>
  
  <p>Project entity:</p>
  
  <pre><code>/**
   * Project.
   *
   * @ORM\Table(name="project")
   * @ORM\HasLifecycleCallbacks
   * @Gedmo\SoftDeleteable(fieldName="deletedAt")
   * @ORM\Entity(repositoryClass="Artel\ProfileBundle\Entity\Repository\ProjectRepository")
  * @ExclusionPolicy("all")
  */
  class Project
  {
  const STATE_TO_BE_INDEXED_IN_ELA_NOT_APPROVED = 'not_approved';
  const STATE_TO_BE_INDEXED_IN_ELA_CLOSED_BY_CLIENT = 'closed_by_client';
  
  use Timestampable;
  /**
   * @var int
   *
   * @ORM\Column(name="id", type="integer")
   * @ORM\Id
   * @Expose()
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  private $id;
  
  /**
   * @ORM\Column(name="current_status", type="string", length=100, nullable = true)
   * @Expose()
   * @Type("string")
   */
  protected $currentStatus = 'not_approved';
  
  /**
   * @ORM\OneToMany(targetEntity="CodeEvents", mappedBy="project", cascade={"persist", "remove"})
   */
  protected $events;
  
  /**
   * @ORM\PrePersist()
   */
  public function PrePersist(){
      if($this-&gt;getCurrentStatus() == self::STATE_TO_BE_INDEXED_IN_ELA_CLOSED_BY_CLIENT){
          $event = new CodeEvents();
          $event
              -&gt;setProject($this)
              -&gt;setEvent(self::STATE_TO_BE_INDEXED_IN_ELA_CLOSED_BY_CLIENT)
          ;
      }
  }
  
  /**
   * @ORM\PreFlush()
   */
  public function PreFlush(){
      if($this-&gt;getCurrentStatus() == self::STATE_TO_BE_INDEXED_IN_ELA_CLOSED_BY_CLIENT){
          $event = new CodeEvents();
          $event
              -&gt;setProject($this)
              -&gt;setEvent(self::STATE_TO_BE_INDEXED_IN_ELA_CLOSED_BY_CLIENT)
          ;
      }
  }
  </code></pre>
  
  <p>I think that I could maybe do something like this, but entity <code>Event</code> not flush in DB:</p>
  
  <pre><code>/**
   * @ORM\PrePersist()
   */
  public function PrePersist(){
      if($this-&gt;getCurrentStatus() == self::STATE_TO_BE_INDEXED_IN_ELA_CLOSED_BY_CLIENT){
          $event = new CodeEvents();
          $event
              -&gt;setProject($this)
              -&gt;setEvent(self::STATE_TO_BE_INDEXED_IN_ELA_CLOSED_BY_CLIENT)
          ;
      }
  }
  
  /**
   * @ORM\PreFlush()
   */
  public function PreFlush(){
      if($this-&gt;getCurrentStatus() == self::STATE_TO_BE_INDEXED_IN_ELA_CLOSED_BY_CLIENT){
          $event = new CodeEvents();
          $event
              -&gt;setProject($this)
              -&gt;setEvent(self::STATE_TO_BE_INDEXED_IN_ELA_CLOSED_BY_CLIENT)
          ;
      }
  }
  </code></pre>
  
  <p>I add </p>
  
  <pre><code>    /**
   * @ORM\PreFlush()
   */
  public function PreFlush(LifecycleEventArgs $event){
      $entityManager = $event-&gt;getEntityManager();
      if($this-&gt;getCurrentStatus() == self::STATE_TO_BE_INDEXED_IN_ELA_CLOSED_BY_CLIENT){
          $event = new CodeEvents();
          $event
              -&gt;setProject($this)
              -&gt;setEvent(self::STATE_TO_BE_INDEXED_IN_ELA_CLOSED_BY_CLIENT)
          ;
          $entityManager-&gt;persist($event);
          $entityManager-&gt;flush();
      }
  }
  </code></pre>
  
  <p>but when flush entity have error</p>
  
  <pre><code>`Catchable Fatal Error: Argument 1 passed to Proxies\__CG__\Artel\ProfileBundle\Entity\Project::PreFlush() must be an instance of Doctrine\ORM\Event\LifecycleEventArgs, instance of Doctrine\ORM\Event\PreFlushEventArgs given, called in /home/ivan/host/aog-code/vendor/doctrine/orm/lib/Doctrine/ORM/Event/ListenersInvoker.php on line 102 and defined`
  </code></pre>
  
  <p>How to create listener for event - entity Project change status for 'closed_by_client' - create entity Event? </p>
  
  <p>I did not create early event, how to create event for this business logic ?</p>