public function setup()
    {
        $app = $this->getApplication();

        $options = $app->config('doctrine');
        if (is_array($options)) {
            $this->setOptions($this->options, $options);
        }

        $proxyDir = $this->getOption('proxy_path');
        $cache = DoctrineCacheFactory::configureCache($this->getOption('cache_driver'));

        $config = Setup::createConfiguration(!!$app->config('debug'), $proxyDir, $cache);
        $config->setNamingStrategy(new UnderscoreNamingStrategy());

        $this->setupAnnotationMetadata();

        if (!$this->setupMetadataDriver($config)) {
            throw new \RuntimeException('No Metadata Driver defined');
        }
        $config->setAutoGenerateProxyClasses($this->getOption('auto_generate_proxies', true) == true);

        $connection = $this->getOption('connection');

        $app->container->singleton(
            'entityManager',
            function () use ($connection, $config) {
                return EntityManager::create($connection, $config);
            }
        );
    }