Last active
August 29, 2015 14:27
-
-
Save jeremykendall/442b840df76ffca72a8d to your computer and use it in GitHub Desktop.
Doctrine2 EntityManager via Aura.Di using wrapper and factory
This file contains hidden or 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 | |
namespace Namespace\Di; | |
use Aura\Di\Config; | |
use Aura\Di\Container; | |
use Doctrine\Common\Cache\ApcCache; | |
use Doctrine\Common\Cache\ArrayCache; | |
use Doctrine\DBAL\Event\Listeners\MysqlSessionInit; | |
use Doctrine\ORM\Configuration; | |
use Doctrine\ORM\EntityManager; | |
class DoctrineConfig extends Config | |
{ | |
public function define(Container $di) | |
{ | |
parent::define($di); | |
// ... snip $connectionOptions and $config ... | |
$di->set('entityManager', $di->lazy(array('Doctrine\ORM\EntityManager', 'create'), $connectionOptions, $config)); | |
} | |
public function modify(Container $di) | |
{ | |
parent::modify($di); | |
$di->get('entityManager')->getEventManager()->addEventSubscriber( | |
new MysqlSessionInit('utf8', 'utf8_unicode_ci') | |
); | |
} | |
protected function getCacheImpl() | |
{ | |
if (APP_MODE === 'development') { | |
return new ArrayCache(); | |
} | |
return new ApcCache(); | |
} | |
protected function shouldAutoGenerateProxies() | |
{ | |
return (APP_MODE === 'development'); | |
} | |
} |
This file contains hidden or 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 | |
// Default EntityManager | |
$em1 = $di->get('entityManager'); | |
$em2 = $di->get('entityManager'); | |
var_dump($em1 === $em2); // true | |
// Grab a new EntityManager using Container::newFactory() rather than Container::newInstance() | |
$newEm = $di->newFactory('Doctrine\ORM\EntityManager'); | |
var_dump($em1 !== $newEm); // true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As you will be getting different instances of entity manager. I feel this https://gist.github.com/jeremykendall/442b840df76ffca72a8d/446a2f79019e0ecb87679cee88728f26119b5225#file-doctrineconfig-php-L38-L40 line should be moved inside to the Factory.