Still trying to figure out the best way but that's what I have so far. Decided to keep it separate for now in it's own module Doctrine. The only file in the module is Module.php. Of course don't forget to register it in appConfig.
Here's the code:
namespace Doctrine2;
use Zend\EventManager\StaticEventManager,
Zend\Module\Consumer\AutoloaderProvider,
Zend\Registry;
class Module implements AutoloaderProvider
{
public function init()
{
$events = StaticEventManager::getInstance();
$events->attach('bootstrap', 'bootstrap', array($this, 'initializeDoctrine'), 100);
}
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
'Doctrine\Common' => VENDOR_PATH . '/doctrine2-orm/lib/vendor/doctrine-common/lib/Doctrine/Common',
'Doctrine\DBAL' => VENDOR_PATH . '/doctrine2-orm/lib/vendor/doctrine-dbal/lib/Doctrine/DBAL',
'Doctrine' => VENDOR_PATH . '/doctrine2-orm/lib/Doctrine',
)
)
);
}
public function initializeDoctrine()
{
$config = new \Doctrine\ORM\Configuration();
$modelsPath = APPLICATION_PATH . '/modules/Application/src/Application/Model';
// Proxy Configuration
$config->setProxyDir($modelsPath . '/Proxies');
$config->setProxyNamespace('Application\Models');
$config->setAutoGenerateProxyClasses((APPLICATION_ENV == "development"));
// Mapping Configuration
$driverImpl = $config->newDefaultAnnotationDriver($modelsPath);
$config->setMetadataDriverImpl($driverImpl);
// database configuration parameters
$conn = array(
'driver' => 'pdo_mysql',
'dbname' => 'db-dev',
'charset' => 'UTF-8'
);
// obtaining the entity manager
$evm = new \Doctrine\Common\EventManager();
$entityManager = \Doctrine\ORM\EntityManager::create($conn, $config, $evm);
Registry::set('doctrine-em', $entityManager);
}
}