title: How to make sure Synfony Entity is loaded to trigger postLoad event?
body: <p>I'm new to Symfony and following <a href="http://jobeet.symfony.gr.jp" rel="nofollow">the Jobeet tutorial</a>. I'm trying to inject Container into Entity using service listener and <code>postLoad</code>. The purpose is to use <a href="https://github.com/liip/LiipImagineBundle/" rel="nofollow">LiipImagineBundle</a> to write a thumbnail image in <code>@ORM\PostPersist</code>. The problem is that the Job entity is not loaded on some routes and then <code>postLoad</code> is not triggered. So, the container which is used in <code>@ORM\PostPersist</code> is not available.</p>
  
  <p>According to the tutorial, there are two routes where <code>PostPersist</code> run:</p>
  
  <ul>
  <li><code>job/create</code></li>
  <li><code>job/update</code></li>
  </ul>
  
  <p>I found that the Job entity is not loaded and <code>postLoad</code> is not triggered on <code>job/create</code>. The route <code>job/update</code> is fine and the container is injected. I posted some code snippets of</p>
  
  <ul>
  <li>services.yml</li>
  <li>JobListener.php</li>
  <li>Entity\Job.php</li>
  <li>JobController.php</li>
  </ul>
  
  <p><strong>src/Ibw/JobeetBundle/Resources/config/services.yml</strong></p>
  
  <pre><code>services:
      ibw.jobeet.entity.job.container_aware:
          class: Ibw\JobeetBundle\Doctrine\Event\Listener\JobListener
          calls:
              - [setContainer, ["@service_container"]]
          tags:
              - { name: doctrine.event_listener, event: postLoad }
  </code></pre>
  
  <p><strong>src/Ibw/JobeetBundle/Doctrine/Event/Listener/JobListener.php</strong></p>
  
  <pre><code>&lt;?php
  namespace Ibw\JobeetBundle\Doctrine\Event\Listener;
  
  use Symfony\Component\DependencyInjection\ContainerInterface;
  use Doctrine\ORM\Event\LifecycleEventArgs;
  
  class JobListener
  {
      /** @var ContainerInterface */
      protected $container;
  
      /**
      * @param ContainerInterface @container
      */
      public function setContainer(ContainerInterface $container)
      {
          $this-&gt;container = $container;
      }
  
      /**
       * @ORM\PostLoad
       */
      public function postLoad(LifecycleEventArgs $eventArgs)
      {
          $entity = $eventArgs-&gt;getEntity();
          if (method_exists($entity, 'setContainer')) {
              $entity-&gt;setContainer($this-&gt;container);
          }
      }
  }
  </code></pre>
  
  <p><strong>src/Ibw/JobeetBundle/Entity/Job.php</strong></p>
  
  <pre><code>// ....
  /**
   * @ORM\PostPersist
   */
  public function upload()
  {
      if (null === $this-&gt;getFile()) {
          return;
      }
  
      if ($this-&gt;getFile()-&gt;move($this-&gt;getUploadTmpRootDir(), $this-&gt;logo)) {
          $this-&gt;moveAndResizeImage();
      }
  
      $this-&gt;file = null;
  }
  
  private function moveAndResizeImage($filter = 'primary')
  {
      $path = $this-&gt;getWebTmpPath();
      $target = $this-&gt;getAbsolutePath();
  
      if (file_exists($path)) {
          $container = $this-&gt;container; 
          // got problem here. $container is null for /job/create
          // but it is okay for /job/update
          $dataManager = $container-&gt;get('liip_imagine.data.manager');
          $filterManager = $container-&gt;get('liip_imagine.filter.manager');
  
          $image = $dataManager-&gt;find($filter, $path); 
          $thumb = $filterManager-&gt;applyFilter($image, $filter);
  
          file_put_contents($target, $thumb-&gt;getContent());
  
          unlink($path); // remove the temp client original image
      }
  }
  // ...
  </code></pre>
  
  <p><strong>src/Ibw/JobeetBundle/Controller/JobController.php</strong></p>
  
  <pre><code>// ...
  public function createAction(Request $request)
  {
      $entity = new Job();
      $form = $this-&gt;createCreateForm($entity);
      $form-&gt;handleRequest($request);
  
      if ($form-&gt;isValid()) {
          $em = $this-&gt;getDoctrine()-&gt;getManager();
          $em-&gt;persist($entity);
          $em-&gt;flush();
  
          return $this-&gt;redirect($this-&gt;generateUrl('ibw_job_preview', array(
              'company' =&gt; $entity-&gt;getCompanySlug(),
              'location' =&gt; $entity-&gt;getLocationSlug(),
              'token' =&gt; $entity-&gt;getToken(),
              'position' =&gt; $entity-&gt;getPositionSlug()
          )));
      }
  
      return $this-&gt;render('IbwJobeetBundle:Job:new.html.twig', array(
          'entity' =&gt; $entity,
          'form'   =&gt; $form-&gt;createView(),
      ));
  }
  // ....
  </code></pre>
  
  <p>Is there any way to solve this problem?</p>