protected function createEntityManager()
	{
		// Cache can be null in case of auto setup
		if ($cache = $this->getConfig('cache_driver', 'array'))
		{
			$cache = 'doctrine.cache.'.$cache;

			$cache = DiC::resolve($cache);
		}

		// Auto or manual setup
		if ($this->getConfig('auto_config', false))
		{
			$dev = $this->getConfig('dev_mode', \Fuel::$env === \Fuel::DEVELOPMENT);
			$proxy_dir = $this->getConfig('proxy_dir');

			$config = Setup::createConfiguration($dev, $proxy_dir, $cache);
		}
		else
		{
			$config = new Configuration;

			$config->setProxyDir($this->getConfig('proxy_dir'));
			$config->setProxyNamespace($this->getConfig('proxy_namespace'));
			$config->setAutoGenerateProxyClasses($this->getConfig('auto_generate_proxy_classes', false));

			if ($cache)
			{
				$config->setMetadataCacheImpl($cache);
				$config->setQueryCacheImpl($cache);
				$config->setResultCacheImpl($cache);
			}
		}

		// Ugly hack for autoloading annotations
		$config->newDefaultAnnotationDriver(array());

		$this->registerMapping($config);

		$conn = DiC::multiton('dbal', $this->getConfig('dbal'), [$this->getConfig('dbal')]);
		$evm = $conn->getEventManager();

		$this->registerBehaviors($evm, $config);

		return $this->entityManager = EntityManager::create($conn, $config, $evm);
	}