Skip to content

Instantly share code, notes, and snippets.

@Ocramius
Created April 18, 2011 08:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ocramius/925033 to your computer and use it in GitHub Desktop.
Save Ocramius/925033 to your computer and use it in GitHub Desktop.
Application_Model_Resource_Doctrine Doctrine2 EntityManager generator resource
<?php
class Application_Model_Resource_Doctrine extends Zend_Application_Resource_ResourceAbstract {
/**
*
* @var \Application_Model_Resource_Doctrine
*/
protected static $instance = null;
/**
* @var string
*/
public $_explicitType = 'em';
/**
* @var \Doctrine\ORM\EntityManager
*/
protected $_em;
/**
*
* @return \Doctrine\ORM\EntityManager
*/
public function init() {
if(null === self::$instance) {
self::$instance = $this;
$options = $this->getOptions();
$config = new \Doctrine\ORM\Configuration;
//Additional annotations
if(isset($options['additionalTypeMappings'])) {
$typeMappings = $options['additionalTypeMappings'];
if(!\is_array($typeMappings)) {
$typeMappings = array($typeMappings);
}
foreach($typeMappings as $typeMapping) {
\Doctrine\DBAL\Types\Type::addType($typeMapping::getMappingName(), $typeMapping);
}
}
//Metadata
$metadataCache = new $options['metadataCacheImpl']['class'];
$metadataCache->setNamespace($options['metadataCacheImpl']['namespace']);
$config->setMetadataCacheImpl($metadataCache);
$reader = $this->getBootstrap()->getApplication()->getBootstrap()->getResource('annotation-reader');
$config->setMetadataDriverImpl(
new \Doctrine\ORM\Mapping\Driver\AnnotationDriver(
$reader,
(array) $options['metadataDriverImpl']['options']['path']
)
);
//Logger
if((bool) $options['appendLogger']) {
$config->setSQLLogger(new \Deneb\Doctrine\SQLLogger());
}
//Query Cache
$queryCache = new $options['queryCacheImpl']['class'];
$queryCache->setNamespace($options['queryCacheImpl']['namespace']);
$config->setQueryCacheImpl($queryCache);
$resultCache = new $options['resultCacheImpl']['class'];
$resultCache->setNamespace($options['resultCacheImpl']['namespace']);
$config->setResultCacheImpl($resultCache);
//Proxies and proxies autoloading
$proxies_dir = realpath($options['proxies']['dir']);
set_include_path(get_include_path() . PATH_SEPARATOR . $proxies_dir);
$this
->getBootstrap()
->getApplication()
->getAutoloader()
->registerNamespace($options['proxies']['namespace']);
$config->setProxyDir($proxies_dir . DIRECTORY_SEPARATOR . $options['proxies']['namespace']);
$config->setProxyNamespace((string) $options['proxies']['namespace']);
$config->setAutoGenerateProxyClasses((bool) $options['proxies']['autogenerate']);
//EntityManager
$this->_em = Doctrine\ORM\EntityManager::create(
$options['connectionOptions'],
$config
);
}
return $this->_em;
}
/**
*
* @return \Doctrine\ORM\EntityManager
*/
public static function getInstance() {
if(self::$instance === null) {
self::$instance = new self();
}
return self::$instance->init();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment