title: Symfony2 access Doctrine Entity Manager in custom class
body: <p>UPDATE:</p>
  
  <p>In case you need to work with Entity Manager in a custom class, you could go this way:</p>
  
  <p>put this code in your bundle:</p>
  
  <pre class="lang-php prettyprint-override"><code>use Symfony\Component\DependencyInjection\Container;
  use Symfony\Component\HttpFoundation\Request;
  require_once DIR . '/../../../app/bootstrap.php.cache';
  require_once DIR . '/../../../app/AppKernel.php';
  
  class ApplicationBoot {
    private static $kernel;
  
    public static function getContainer() {
      if(self::$kernel instanceof \AppKernel) {
        if(!self::$kernel-&gt;getContainer() instanceof Container){
          self::$kernel-&gt;boot();
      }            
      return self::$kernel-&gt;getContainer();
    }
  
    $environment = 'prod';
    if (!array_key_exists('REMOTE_ADDR', $_SERVER) || in_array(@$_SERVER['REMOTE_ADDR'], array('127.0.0.1', '::1', 'localhost'))) {
      $environment = 'dev';
    }
  
    self::$kernel = new \AppKernel($environment, false);
    self::$kernel-&gt;boot();
    return self::$kernel-&gt;getContainer();
    }
  
    public static function shutDown() {
      self::$kernel-&gt;shutdown();
  }}
  </code></pre>
  
  <p>So now you can access EntityManager:</p>
  
  <pre class="lang-php prettyprint-override"><code>$container = ApplicationBoot::getContainer();
  $entityManager = $container-&gt;get('doctrine')-&gt;getEntityManager();
  </code></pre>