-
-
Save meotimdihia/4d927e349a9bfa9c2674 to your computer and use it in GitHub Desktop.
a.php
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\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']]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment