Skip to content

Instantly share code, notes, and snippets.

@jansenfelipe
Created July 31, 2014 21:00
Show Gist options
  • Save jansenfelipe/06cfbeccf991bdf631a3 to your computer and use it in GitHub Desktop.
Save jansenfelipe/06cfbeccf991bdf631a3 to your computer and use it in GitHub Desktop.
<?php
use Doctrine\Common\ClassLoader,
Doctrine\ORM\Tools\Setup,
Doctrine\ORM\EntityManager;
class Doctrine {
public $em;
public function __construct() {
Doctrine\Common\Annotations\AnnotationRegistry::registerFile(
__DIR__ . '/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php'
);
// Load the database configuration from CodeIgniter
require __DIR__ . '/../config/' . ENVIRONMENT . '/database.php';
$connection_options = array(
'driver' => 'pdo_'.$db['default']['dbdriver'],
'user' => $db['default']['username'],
'password' => $db['default']['password'],
'host' => $db['default']['hostname'],
'dbname' => $db['default']['database'],
'charset' => $db['default']['char_set'],
'driverOptions' => array(
'charset' => $db['default']['char_set'],
),
);
// With this configuration, your model files need to be in application/models/Entity
// e.g. Creating a new Entity\User loads the class from application/models/Entity/User.php
$models_namespace = 'Entity';
$models_path = __DIR__ . '/../models';
$proxies_dir = __DIR__ . '/../models/Proxies';
$metadata_paths = array(__DIR__ . '/../models/xml-mappings');
// Set $dev_mode to TRUE to disable caching while you develop
$config = new Doctrine\ORM\Configuration;
$config->setProxyDir(sys_get_temp_dir());
$config->setProxyNamespace('Proxy');
//$config = Setup::createXMLMetadataConfiguration($metadata_paths, ENVIRONMENT == 'development', $proxies_dir);
$driverChain = new Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain();
$driverChain->addDriver(new Doctrine\ORM\Mapping\Driver\XmlDriver(__DIR__ . '/../models/xml-mappings'), 'Entity');
Gedmo\DoctrineExtensions::registerAbstractMappingIntoDriverChainORM($driverChain);
$config->setMetadataDriverImpl($driverChain);
$evm = new Doctrine\Common\EventManager();
$timestampableListener = new Gedmo\Timestampable\TimestampableListener;
$evm->addEventSubscriber($timestampableListener);
$evm->addEventSubscriber(new Doctrine\DBAL\Event\Listeners\MysqlSessionInit());
$this->em = EntityManager::create($connection_options, $config, $evm);
$loader = new ClassLoader($models_namespace, $models_path);
$loader->register();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment