Created
June 19, 2012 13:24
-
-
Save Hounddog/2954132 to your computer and use it in GitHub Desktop.
current entity resource
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Doctrine\ORM\EntityManager, | |
Doctrine\ORM\Configuration, | |
Doctrine\DBAL\Event\Listeners\MysqlSessionInit, | |
Doctrine\Common\Cache\ArrayCache, | |
Doctrine\Common\Annotations\AnnotationRegistry, | |
Doctrine\Common\Annotations\AnnotationReader, | |
Doctrine\Common\Annotations\CachedReader, | |
Doctrine\Common\Annotations\IndexedReader, | |
Doctrine\ORM\Mapping\Driver\Driver, | |
Doctrine\ORM\Mapping\Driver\AnnotationDriver; | |
/** | |
* @package Pwb | |
* @copyright 2010 Doyousoft | |
* @version $Id: Entitymanager.php,v 1.8 2012-02-03 10:36:48 sebc Exp $ | |
*/ | |
class Pwb_Resource_Entitymanager extends | |
Zend_Application_Resource_ResourceAbstract | |
{ | |
protected $_options = array(); | |
public function init() | |
{ | |
$options = $this->getOptions(); | |
$config = new Configuration; | |
$config->setProxyDir($options['proxyDir']); | |
$config->setProxyNamespace($options['proxyNamespace']); | |
// config annotations | |
AnnotationRegistry::registerFile( | |
'Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php' | |
); | |
$reader = new CachedReader( | |
new IndexedReader(new AnnotationReader()), | |
new ArrayCache() | |
); | |
$driverImpl = new AnnotationDriver( | |
$reader, | |
array(APPLICATION_PATH . '/modules') | |
); | |
// application own annotations | |
// TODO : namespace it when the application will use namespace | |
AnnotationReader::addGlobalIgnoredName('service'); | |
AnnotationReader::addGlobalIgnoredName('transport'); | |
AnnotationReader::addGlobalIgnoredName('envelope'); | |
AnnotationReader::addGlobalIgnoredName('contentType'); | |
AnnotationReader::addGlobalIgnoredName('dto'); | |
$config->setMetadataDriverImpl($driverImpl); | |
$config->setAutoGenerateProxyClasses( | |
$options['autoGenerateProxyClasses'] | |
); | |
//Caching | |
if(Zend_Registry::get('cacheEnabled')) { | |
$cacheDriver = new \Doctrine\Common\Cache\ApcCache(); | |
$config->setQueryCacheImpl($cacheDriver); | |
$config->setMetadataCacheImpl($cacheDriver); | |
$config->setResultCacheImpl($cacheDriver); | |
} | |
$configDb = $this->getBootstrap()->getResource('db')->getConfig(); | |
$entityManager = EntityManager::create( | |
array( | |
'driver' => 'pdo_mysql', | |
'host' => $configDb['host'], | |
'dbname' => $configDb['dbname'], | |
'user' => $configDb['username'], | |
'password' => $configDb['password'] | |
), | |
$config | |
); | |
$entityManager->getEventManager()->addEventSubscriber( | |
new MysqlSessionInit($configDb['charset']) | |
); | |
if (isset($options['sqlLog']) && $options['sqlLog']) { | |
$logger = new Pwb_Resource_SqlLogger; | |
$logger->setConnection($entityManager->getConnection()); | |
$config->setSQLLogger($logger); | |
} | |
Zend_Registry::set('entityManager', $entityManager); | |
return $entityManager; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment