Last active
October 1, 2015 23:32
-
-
Save pdt256/a2dccb77584514cf32c5 to your computer and use it in GitHub Desktop.
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 path\to\Lib; | |
use Doctrine; | |
use path\to\Doctrine\Extensions\TablePrefix; | |
class DoctrineHelper | |
{ | |
protected $tablePrefix = 'yourprefix_'; | |
protected $eventManager; | |
/** @var Doctrine\ORM\EntityManager */ | |
protected $entityManager; | |
/** @var Doctrine\DBAL\Configuration */ | |
protected $entityManagerConfiguration; | |
/** @var Doctrine\Common\Cache\CacheProvider */ | |
protected $cacheDriver; | |
/** @var Doctrine\DBAL\Configuration */ | |
protected $config; | |
public function __construct(Doctrine\Common\Cache\CacheProvider $cacheDriver = null) | |
{ | |
$paths = [__DIR__ . '/../Entity']; | |
$isDevMode = true; | |
$this->config = Doctrine\ORM\Tools\Setup::createAnnotationMetadataConfiguration($paths, $isDevMode); | |
$xmlDriver = new Doctrine\ORM\Mapping\Driver\XmlDriver(realpath(__DIR__ . '/../Doctrine/Mapping')); | |
$this->config->setMetadataDriverImpl($xmlDriver); | |
$this->config->addEntityNamespace('YourNamespace', 'path\to\Entity'); | |
if ($cacheDriver !== null) { | |
$this->cacheDriver = $cacheDriver; | |
$this->config->setMetadataCacheImpl($this->cacheDriver); | |
$this->config->setQueryCacheImpl($this->cacheDriver); | |
$this->config->setResultCacheImpl($this->cacheDriver); | |
} | |
$tablePrefix = new TablePrefix($this->tablePrefix); | |
$this->eventManager = new Doctrine\Common\EventManager; | |
$this->eventManager->addEventListener(Doctrine\ORM\Events::loadClassMetadata, $tablePrefix); | |
} | |
public function clearCache() | |
{ | |
$this->cacheDriver->deleteAll(); | |
} | |
public function getCacheDriver() | |
{ | |
return $this->cacheDriver; | |
} | |
public function getEntityManager() | |
{ | |
return $this->entityManager; | |
} | |
public function setSqlLogger(Doctrine\DBAL\Logging\SQLLogger $sqlLogger) | |
{ | |
$this->entityManagerConfiguration->setSQLLogger($sqlLogger); | |
} | |
public function setup(array $dbParams) | |
{ | |
$this->entityManager = Doctrine\ORM\EntityManager::create($dbParams, $this->config, $this->eventManager); | |
$this->entityManagerConfiguration = $this->entityManager->getConnection()->getConfiguration(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment