Puzo

PB - library/Doctrine.php

Mar 19th, 2013
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.57 KB | None | 0 0
  1. <?php
  2.  
  3. use Doctrine\Common\ClassLoader,
  4.     Doctrine\ORM\Tools\Setup,
  5.     Doctrine\ORM\EntityManager;
  6.  
  7. class Doctrine
  8. {
  9.  
  10.     public $em;
  11.  
  12.     public function __construct()
  13.     {
  14.         require_once __DIR__ . '/Doctrine/ORM/Tools/Setup.php';
  15.         Setup::registerAutoloadDirectory(__DIR__);
  16.  
  17.         // Load the database configuration from CodeIgniter
  18.         require APPPATH . 'config/database.php';
  19.  
  20.         $connection_options = array(
  21.             'driver'        => 'pdo_mysql',
  22.             'user'          => $db['default']['username'],
  23.             'password'      => $db['default']['password'],
  24.             'host'          => $db['default']['hostname'],
  25.             'dbname'        => $db['default']['database'],
  26.             'charset'       => $db['default']['char_set'],
  27.             'driverOptions' => array(
  28.                 1002 =>'SET NAMES utf8'
  29.             ),
  30.         );
  31.  
  32.         // With this configuration, your model files need to be in application/models/Entity
  33.         // e.g. Creating a new Entity\User loads the class from application/models/Entity/User.php
  34.         $models_namespace = 'Entity';
  35.         $models_path = APPPATH . 'models';
  36.         $proxies_dir = APPPATH . 'models/Proxies';
  37.         $metadata_paths = array(APPPATH . 'models');
  38.  
  39.         // Set $dev_mode to TRUE to disable caching while you develop
  40.         $config = Setup::createAnnotationMetadataConfiguration($metadata_paths, $dev_mode = true, $proxies_dir);
  41.         $this->em = EntityManager::create($connection_options, $config);
  42.  
  43.         $loader = new ClassLoader($models_namespace, $models_path);
  44.         $loader->register();
  45.     }
  46. }
  47.  
  48. /*
  49. use Doctrine\Common\ClassLoader,
  50.     Doctrine\ORM\Configuration,
  51.     Doctrine\ORM\EntityManager,
  52.     Doctrine\Common\Cache\ArrayCache,
  53.     Doctrine\DBAL\Logging\EchoSQLLogger;
  54.  
  55. class Doctrine {
  56.  
  57.   public $em = null;
  58.  
  59.   public function __construct()
  60.   {
  61.     // load database configuration from CodeIgniter
  62.     require_once APPPATH.'config/database.php';
  63.  
  64.     // Set up class loading. You could use different autoloaders, provided by your favorite framework,
  65.     // if you want to.
  66.     require_once APPPATH.'libraries/Doctrine/Common/ClassLoader.php';
  67.  
  68.     $doctrineClassLoader = new ClassLoader('Doctrine',  APPPATH.'libraries');
  69.     $doctrineClassLoader->register();
  70.     $entitiesClassLoader = new ClassLoader('models', rtrim(APPPATH, "/" ));
  71.     $entitiesClassLoader->register();
  72.     $proxiesClassLoader = new ClassLoader('Proxies', APPPATH.'models/proxies');
  73.     $proxiesClassLoader->register();
  74.  
  75.     // Set up caches
  76.     $config = new Configuration;
  77.     $cache = new ArrayCache;
  78.     $config->setMetadataCacheImpl($cache);
  79.     $driverImpl = $config->newDefaultAnnotationDriver(array(APPPATH.'models/Entities'));
  80.     $config->setMetadataDriverImpl($driverImpl);
  81.     $config->setQueryCacheImpl($cache);
  82.  
  83.     $config->setQueryCacheImpl($cache);
  84.  
  85.     // Proxy configuration
  86.     $config->setProxyDir(APPPATH.'/models/proxies');
  87.     $config->setProxyNamespace('Proxies');
  88.  
  89.     // Set up logger
  90.     $logger = new EchoSQLLogger;
  91.     $config->setSQLLogger($logger);
  92.  
  93.     $config->setAutoGenerateProxyClasses( TRUE );
  94.  
  95.     // Database connection information
  96.     $connectionOptions = array(
  97.         'driver' => 'pdo_mysql',
  98.         'user' =>     $db['default']['username'],
  99.         'password' => $db['default']['password'],
  100.         'host' =>     $db['default']['hostname'],
  101.         'dbname' =>   $db['default']['database']
  102.     );
  103.  
  104.     // Create EntityManager
  105.     $this->em = EntityManager::create($connectionOptions, $config);
  106.   }
  107. }
  108. */
Advertisement
Add Comment
Please, Sign In to add comment