Skip to content
Create a gist now

Instantly share code, notes, and snippets.

use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\Common\Cache\Cache;
use Doctrine\Common\Cache\ArrayCache;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\Common\Annotations\CachedReader;
use Doctrine\Common\Annotations\AnnotationReader;
class Doctrine{
private $EntityManager;
public function __construct(){
$paths = array(MODELS_DIR . 'Entities');
$isDevMode = true;
$dbParams = array(
'driver' => 'pdo_mysql',
'user' => $this->config['username'],
'password' => $this->config['password'],
'host' => $this->config['host'],
'dbname' => $this->config['dbname'],
'charset' => isset($this->config['charset']) ? $this->config['charset'] : 'UTF8'
$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
new AnnotationDriver(
new CachedReader(
new AnnotationReader(),
new ArrayCache()
$em = EntityManager::create($dbParams, $config);
$conn = $em->getConnection();
$conn->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
$this->EntityManager = $em;
public function getEntityManager(){
return $this->EntityManager;
private function _initConfig()
if (defined('CONFIG_DIR') && file_exists(CONFIG_DIR. DS .'db.conf.php')) {
$configFile = CONFIG_DIR. DS .'db.conf.php';
else {
$configFile = GLOBAL_CONFIGS_DIR .DS .'db.conf.php';
$config = include $configFile;
$this->config = $config['database'][$config['default']];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.