Advertisement
Puzo

Doctrine 2: Bootstrap

Jan 9th, 2012
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.69 KB | None | 0 0
  1. <?php
  2.  
  3. class Doctrine
  4. {
  5.     // the Doctrine entity manager
  6.     public $em = null;
  7.  
  8.     public function __construct()
  9.     {
  10.         // include our CodeIgniter application's database configuration
  11.         require_once APPPATH.'config/database.php';
  12.  
  13.         // include Doctrine's fancy ClassLoader class
  14.         require_once APPPATH.'libraries/Doctrine/Common/ClassLoader.php';
  15.  
  16.         // load the Doctrine classes
  17.         $doctrineClassLoader = new \Doctrine\Common\ClassLoader('Doctrine', APPPATH.'libraries');
  18.         $doctrineClassLoader->register();
  19.        
  20.         // load Symfony2 helpers
  21.         // Don't be alarmed, this is necessary for YAML mapping files
  22.         $symfonyClassLoader = new \Doctrine\Common\ClassLoader('Symfony', APPPATH.'libraries/Doctrine');
  23.         $symfonyClassLoader->register();
  24.  
  25.         // load the entities
  26.         $entityClassLoader = new \Doctrine\Common\ClassLoader('Entities', APPPATH.'models/Entities');
  27.         $entityClassLoader->register();
  28.  
  29.         // load the proxy entities
  30.         $proxyClassLoader = new \Doctrine\Common\ClassLoader('Proxies', APPPATH.'models');
  31.         $proxyClassLoader->register();
  32.  
  33.         // set up the configuration
  34.         $config = new \Doctrine\ORM\Configuration;
  35.    
  36.         if(ENVIRONMENT == 'development')
  37.             // set up simple array caching for development mode
  38.             $cache = new \Doctrine\Common\Cache\ArrayCache;
  39.         else
  40.             // set up caching with APC for production mode
  41.             $cache = new \Doctrine\Common\Cache\ApcCache;
  42.         $config->setMetadataCacheImpl($cache);
  43.         $config->setQueryCacheImpl($cache);
  44.  
  45.         // set up proxy configuration
  46.         $config->setProxyDir(APPPATH.'models/Proxies');
  47.         $config->setProxyNamespace('Proxies');
  48.        
  49.         // auto-generate proxy classes if we are in development mode
  50.         $config->setAutoGenerateProxyClasses(ENVIRONMENT == 'development');
  51.  
  52.         // set up annotation driver
  53.         $yamlDriver = new \Doctrine\ORM\Mapping\Driver\YamlDriver(APPPATH.'models/Mappings');
  54.         $config->setMetadataDriverImpl($yamlDriver);
  55.  
  56.         // Database connection information
  57.         $connectionOptions = array(
  58.             'driver' => 'pdo_mysql',
  59.             'user' => $db['default']['username'],
  60.             'password' => $db['default']['password'],
  61.             'host' => $db['default']['hostname'],
  62.             'dbname' => $db['default']['database']
  63.         );
  64.        
  65.         // create the EntityManager
  66.         $em = \Doctrine\ORM\EntityManager::create($connectionOptions, $config);
  67.        
  68.         // store it as a member, for use in our CodeIgniter controllers.
  69.         $this->em = $em;
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement