Example #1
0
 *
 * 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);
Example #2
0
 /**
  * __constructor, you can specify which database group to use (default: 'default')
  *
  * @param string $database_group
  */
 public function __construct($database_group = 'default')
 {
     // if config was not set by init.php, load it
     if (self::$doctrineConfig === NULL) {
         self::$doctrineConfig = Kohana::$config->load('doctrine');
     }
     $isDevMode = self::$doctrineConfig['debug'];
     $config = Setup::createConfiguration($isDevMode);
     // proxy configuration
     $config->setProxyDir(self::$doctrineConfig['proxy_dir']);
     $config->setProxyNamespace(self::$doctrineConfig['proxy_namespace']);
     $config->setAutoGenerateProxyClasses($isDevMode);
     // String extensions
     foreach (Arr::get(self::$doctrineConfig->get('enabled_extensions', array()), 'string', array()) as $name => $class) {
         $config->addCustomStringFunction($name, $class);
     }
     // caching configuration
     $cache_class = '\\Doctrine\\Common\\Cache\\' . self::$doctrineConfig['cache_implementation'];
     $cache_implementation = new $cache_class();
     // set namespace on cache
     if ($cache_namespace = self::$doctrineConfig['cache_namespace']) {
         $cache_implementation->setNamespace($cache_namespace);
     }
     $config->setMetadataCacheImpl($cache_implementation);
     $config->setQueryCacheImpl($cache_implementation);
     $config->setResultCacheImpl($cache_implementation);
     // mappings/metadata driver configuration
     $driver_implementation = NULL;
     switch (self::$doctrineConfig['mappings_driver']) {
         case 'php':
             $driver_implementation = new PHPDriver(array(self::$doctrineConfig['mappings_path']));
             break;
         case 'xml':
             $driver_implementation = new XmlDriver(array(self::$doctrineConfig['mappings_path']));
             break;
         case 'annotation':
             $useSimpleAnnotationReader = FALSE;
             $driver_implementation = $config->newDefaultAnnotationDriver(array(self::$doctrineConfig['mappings_path']), $useSimpleAnnotationReader);
             AnnotationRegistry::registerLoader('class_exists');
             break;
         default:
         case 'yaml':
             $driver_implementation = new YamlDriver(array(self::$doctrineConfig['mappings_path']));
             break;
     }
     $config->setMetadataDriverImpl($driver_implementation);
     // load config if not defined
     if (self::$databaseConfig === NULL) {
         self::$databaseConfig = Kohana::$config->load('database');
     }
     // get $database_group config
     $db_config = Arr::GET(self::$databaseConfig, $database_group, array());
     // verify that the database group exists
     if (empty($db_config)) {
         throw new Kohana_Database_Exception('database-group "' . $database_group . '" doesn\'t exists');
     }
     if (strtolower($db_config['type']) == 'pdo') {
         $pdo = new PDO($db_config['connection']['dsn'], $db_config['connection']['username'], $db_config['connection']['password'], array(PDO::ATTR_PERSISTENT => $db_config['connection']['persistent']));
         $connectionOptions = array('pdo' => $pdo, 'dbname' => null);
     } else {
         // database configuration
         $connectionOptions = array('driver' => self::$doctrineConfig['type_driver_mapping'][$db_config['type']], 'host' => $db_config['connection']['hostname'], 'port' => $db_config['connection']['port'], 'dbname' => $db_config['connection']['database'], 'user' => $db_config['connection']['username'], 'password' => $db_config['connection']['password'], 'charset' => $db_config['charset']);
     }
     // create Entity Manager
     $this->evm = new EventManager();
     $this->em = EntityManager::create($connectionOptions, $config, $this->evm);
     // specify the charset for MySQL/PDO
     $driverName = $this->em->getConnection()->getDriver()->getName();
     if ($driverName == 'pdo_mysql') {
         $this->em->getEventManager()->addEventSubscriber(new MysqlSessionInit($db_config['charset'], 'utf8_unicode_ci'));
     } else {
         if ($driverName == 'pdo_pgsql') {
             $this->em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('bytea', 'text');
         }
     }
     //fix enum
     $conn = $this->em->getConnection();
     $conn->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
 }
Example #3
0
// hack to get --database-group and pass it to the Doctrine_ORM constructor
$argv2 = $argv;
foreach ($argv as $pos => $arg) {
    if (strpos($arg, '--database-group') !== FALSE) {
        $parts = explode('=', $arg);
        $database_group = $parts[1];
        unset($argv2[$pos]);
    }
}
$input = new Doctrine_ReadWriteArgvInput($argv2);
if (!$input->hasOption('configuration')) {
    $input->setOption('configuration', Kohana::$config->load('doctrine')->get('configuration'));
}
// end: hack to get --database-group and pass it to the Doctrine_ORM constructor
// create a Doctrine_ORM for one database group
$doctrine_orm = new Doctrine_ORM($database_group);
// add console helpers
$helperSet = new \Symfony\Component\Console\Helper\HelperSet(array('db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($doctrine_orm->getEntityManager()->getConnection()), 'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($doctrine_orm->getEntityManager())));
// create and run Symfony Console application
$cli = new Symfony\Component\Console\Application('Kohana Doctrine Command Line Interface</info>' . PHP_EOL . '<comment>use --database-group to specifify another group from database.php (defaut: default)</comment>' . PHP_EOL . '<info>Doctrine', \Doctrine\ORM\Version::VERSION);
$cli->setCatchExceptions(true);
// Register All Doctrine Commands
\Doctrine\ORM\Tools\Console\ConsoleRunner::addCommands($cli);
// Adding own helpers
foreach (Kohana::$config->load('doctrine')->get('console_helpers', array()) as $alias => $helper) {
    $helperSet->set($helper);
}
// Set helperSet
$cli->setHelperSet($helperSet);
// Run with helperset and add own commands
\Doctrine\ORM\Tools\Console\ConsoleRunner::run($helperSet, Kohana::$config->load('doctrine')->get('console_commands', array()));