secret
Created

a.php

  • Download Gist
gistfile1.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
<?php
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(){
$this->_initConfig();
$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);
$config->setMetadataDriverImpl(
new AnnotationDriver(
new CachedReader(
new AnnotationReader(),
new ArrayCache()
),
$paths
)
);
$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']];
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.