Last active
December 23, 2015 21:29
-
-
Save leonguyen/6696118 to your computer and use it in GitHub Desktop.
CI Lab: Create Doctrine bootstrap
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 | |
class Doctrine | |
{ | |
// the Doctrine entity manager | |
public $em = null; | |
public function __construct() | |
{ | |
// include our CodeIgniter application's database configuration | |
require APPPATH.'config/database.php'; | |
// include Doctrine's fancy ClassLoader class | |
require_once APPPATH.'libraries/Doctrine/Common/ClassLoader.php'; | |
// load the Doctrine classes | |
$doctrineClassLoader = new \Doctrine\Common\ClassLoader('Doctrine', APPPATH.'libraries'); | |
$doctrineClassLoader->register(); | |
// load Symfony2 helpers | |
// Don't be alarmed, this is necessary for YAML mapping files | |
$symfonyClassLoader = new \Doctrine\Common\ClassLoader('Symfony', APPPATH.'libraries/Doctrine'); | |
$symfonyClassLoader->register(); | |
// load the entities | |
$entityClassLoader = new \Doctrine\Common\ClassLoader('Entities', APPPATH.'models'); | |
$entityClassLoader->register(); | |
// load the proxy entities | |
$proxyClassLoader = new \Doctrine\Common\ClassLoader('Proxies', APPPATH.'models'); | |
$proxyClassLoader->register(); | |
// set up the configuration | |
$config = new \Doctrine\ORM\Configuration; | |
if(ENVIRONMENT == 'development') | |
// set up simple array caching for development mode | |
$cache = new \Doctrine\Common\Cache\ArrayCache; | |
else | |
// set up caching with APC for production mode | |
$cache = new \Doctrine\Common\Cache\ApcCache; | |
$config->setMetadataCacheImpl($cache); | |
$config->setQueryCacheImpl($cache); | |
// set up proxy configuration | |
$config->setProxyDir(APPPATH.'models/Proxies'); | |
$config->setProxyNamespace('Proxies'); | |
// auto-generate proxy classes if we are in development mode | |
$config->setAutoGenerateProxyClasses(ENVIRONMENT == 'development'); | |
// set up annotation driver | |
$yamlDriver = new \Doctrine\ORM\Mapping\Driver\YamlDriver(APPPATH.'models/Mappings'); | |
$config->setMetadataDriverImpl($yamlDriver); | |
// Database connection information | |
$connectionOptions = array( | |
'driver' => 'pdo_mysql', | |
'user' => $db['default']['username'], | |
'password' => $db['default']['password'], | |
'host' => $db['default']['hostname'], | |
'dbname' => $db['default']['database'] | |
); | |
// create the EntityManager | |
$em = \Doctrine\ORM\EntityManager::create($connectionOptions, $config); | |
// store it as a member, for use in our CodeIgniter controllers. | |
$this->em = $em; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment