Example #1
0
 * kohana-doctrine initialization, executed by application bootstrap.php
 *
 * LICENSE: THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). THE WORK IS PROTECTED
 * BY COPYRIGHT AND/OR OTHER APPLICABLE LAW. ANY USE OF THE WORK OTHER THAN AS
 * AUTHORIZED UNDER THIS LICENSE OR COPYRIGHT LAW IS PROHIBITED.
 *
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND AGREE TO
 * BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE MAY BE
 * CONSIDERED TO BE A CONTRACT, THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED HERE
 * IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 *
 * @category  module
 * @package   kohana-doctrine
 * @author    gimpe <*****@*****.**> Oleg Abrazhaev <*****@*****.**>
 * @copyright 2011 International Jaywalkers
 * @license   http://creativecommons.org/licenses/by/3.0/ CC BY 3.0
 * @link      http://github.com/seyfer/kohana-doctrine
 */
// include kohana-doctrine config
$doctrine_config = Kohana::$config->load('doctrine');
// Autoload through composer
require_once $doctrine_config['vendor_path'] . '/autoload.php';
// defines your "extensions" namespace
$classLoader = new \Doctrine\Common\ClassLoader('DoctrineExtensions', $doctrine_config['extensions_path']);
$classLoader->register();
// Make proxies autoloader work so they work when seralizing objects.
// Proxies are not PSR-0 compliant.
Doctrine\ORM\Proxy\Autoloader::register($doctrine_config['proxy_dir'], $doctrine_config['proxy_namespace']);
// Re-use already loaded Doctrine config
Doctrine_ORM::setConfig($doctrine_config);
 /**
  * Get Doctrine2
  *
  * @param string $db The database instance to get (if using multiple databases)
  * @return Doctrine\ORM\EntityManager
  */
 public function getDoctrine2($db = 'default')
 {
     if ($this->_doctrine2 === null || !isset($this->_doctrine2[$db])) {
         // Get Doctrine configuration options from the application.ini file
         $dconfig = $this->getOptions();
         if ($db != 'default') {
             $dconfig = $dconfig[$db];
         }
         try {
             if (Zend_Registry::isRegistered('d2cache')) {
                 $cache = Zend_Registry::get('d2cache');
             } else {
                 $d2cacheOptions = $this->getBootstrap()->getApplication()->getOptions()['resources']['doctrine2cache'];
                 if (!$d2cacheOptions || !isset($d2cacheOptions['type'])) {
                     throw new Zend_Exception('force err');
                 }
                 $plugin = new OSS_Resource_Doctrine2cache($d2cacheOptions);
                 $this->getBootstrap()->registerPluginResource($plugin);
                 $cache = $plugin->getDoctrine2cache();
             }
         } catch (Zend_Exception $e) {
             die(_('ERROR: Doctrine2 requires Doctrine2Cache to have been already bootstrapped'));
         }
         $config = new Doctrine\ORM\Configuration();
         $config->setMetadataCacheImpl($cache);
         $driver = new \Doctrine\ORM\Mapping\Driver\XmlDriver(array($dconfig['xml_schema_path']));
         $config->setMetadataDriverImpl($driver);
         $config->setQueryCacheImpl($cache);
         $config->setResultCacheImpl($cache);
         $config->setProxyDir($dconfig['proxies_path']);
         $config->setProxyNamespace($dconfig['proxies_namespace']);
         $config->setAutoGenerateProxyClasses($dconfig['autogen_proxies']);
         if (isset($dconfig['logger']) && $dconfig['logger']) {
             $config->setSQLLogger(new OSS_Doctrine2_FirebugProfiler());
         }
         $this->_doctrine2[$db] = Doctrine\ORM\EntityManager::create($dconfig['connection']['options'], $config);
         $modelAutoLoader = new \Doctrine\Common\ClassLoader($dconfig['models_namespace'], realpath($dconfig['models_path']));
         $repositoryAutoLoader = new \Doctrine\Common\ClassLoader($dconfig['repositories_namespace'], realpath($dconfig['repositories_path']));
         $autoloader = Zend_Loader_Autoloader::getInstance();
         $autoloader->pushAutoloader(array($modelAutoLoader, 'loadClass'), $dconfig['models_namespace']);
         $autoloader->pushAutoloader(array($repositoryAutoLoader, 'loadClass'), $dconfig['repositories_namespace']);
         // http://docs.doctrine-project.org/en/latest/reference/configuration.html#autoloading-proxies
         Doctrine\ORM\Proxy\Autoloader::register($dconfig['proxies_path'], $dconfig['proxies_namespace']);
     }
     return $this->_doctrine2[$db];
 }
Example #3
0
 private function loadDoctrine()
 {
     $loader = new \Doctrine\ORM\Proxy\Autoloader();
     $loader->register(ROOT . '/cache/proxies', 'Proxies');
     if (!$this->container->get('config')->has('db_type')) {
         return;
     }
     $this->container->set('em', $this->container->share(function ($c) {
         $paths = array(__DIR__ . '/../../app/Entity');
         $isDevMode = false;
         $dbParams = array('driver' => 'pdo_' . $c->get('config')->get('db_type'), 'user' => $c->get('config')->get('db_username'), 'password' => $c->get('config')->get('db_password'), 'dbname' => $c->get('config')->get('db_name'), 'host' => $c->get('config')->get('db_host'));
         $config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
         $config->addEntityNamespace('Cookmehome', 'app\\Entity');
         $cache = new \Doctrine\Common\Cache\ArrayCache();
         $config->setMetadataCacheImpl($cache);
         $config->setQueryCacheImpl($cache);
         $logger = new \Doctrine\DBAL\Logging\EchoSQLLogger();
         //$config->setSQLLogger($logger);
         $config->setProxyDir(ROOT . '/cache/proxies');
         $config->setProxyNamespace('Proxies');
         $config->setAutoGenerateProxyClasses(false);
         return EntityManager::create($dbParams, $config);
     }));
 }