title: Set created by user reference on PrePersist in doctrine entity in zf2 project
body: <p>In my zf2 project, I have doctrine 2 entities that reference the user entity as created by as the following:</p>
  
  <pre><code>/**
   * @ORM\ManyToOne(targetEntity="User")
   * @ORM\JoinColumn(name="created_by", referencedColumnName="id")
   **/
  protected $createdBy;
  </code></pre>
  
  <p>and I want to set this reference in the <code>PrePersist</code>, how can I do that?
  I tried the following (I don't know if it is right):</p>
  
  <pre><code>/** @ORM\PrePersist */
  public function prePersist() {
      if ($this-&gt;createdBy === null) {
          $session = new \Zend\Authentication\Storage\Session;
          $userId = $session-&gt;read();
          if ($userId !== null) {
              $this-&gt;createdBy = $userId;
          } else {
              throw new \Exception("Invalid User");
          }
      }
  }
  </code></pre>
  
  <p>but the main problem is that the <code>$userId</code> is an integer, and <code>createdBy</code> must hold the reference of the user not the user ID.</p>
  
  <p>is there a better way to do that? if no, how can I get the reference instead of the user ID?</p>