Skip to content

Instantly share code, notes, and snippets.

@l3pp4rd
Created May 10, 2011 08:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save l3pp4rd/964075 to your computer and use it in GitHub Desktop.
Save l3pp4rd/964075 to your computer and use it in GitHub Desktop.
bootstrap of d2
<?php
use Doctrine\Common\ClassLoader,
Doctrine\ORM\Configuration,
Doctrine\ORM\EntityManager,
Doctrine\Common\Cache\ArrayCache,
Doctrine\DBAL\Logging\EchoSqlLogger;
define('BASEPATH', __DIR__ . '/../system/');
require_once __DIR__ . '/config/database.php';
require_once __DIR__ . '/libraries/Doctrine/Common/ClassLoader.php';
$doctrineClassLoader = new ClassLoader('Doctrine', __DIR__.'libraries');
$doctrineClassLoader->register();
$entitiesClassLoader = new ClassLoader('models', __DIR__);
$entitiesClassLoader->register();
$proxiesClassLoader = new ClassLoader('Proxies', __DIR__.'models/proxies');
$proxiesClassLoader->register();
$classLoader = new \Doctrine\Common\ClassLoader('Gedmo', '../libraries/Doctrine/DoctrineExtensions/lib');
$classLoader->register();
$config = new \Doctrine\ORM\Configuration();
$config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
$config->setQueryCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
$config->setProxyDir(__DIR__ . '/Proxies');
$config->setProxyNamespace('Proxies');
// Set up drivers
$driverChain = new \Doctrine\ORM\Mapping\Driver\DriverChain();
// default annotation driver for all ORM metadata
$annotationDriver = $config->newDefaultAnnotationDriver();
// your xml driverhttp://www.gediminasm.org/article/doctrine-2-on-zend-framework
$xmlDriver = new \Doctrine\ORM\Mapping\Driver\XmlDriver(__DIR__.'/models');
// now register drivers to the chain
$driverChain->addDriver($annotationDriver, 'Gedmo\Translatable');
$driverChain->addDriver($annotationDriver, 'Gedmo\Loggable');
$driverChain->addDriver($xmlDriver, 'Model'); // this is your 'models' directory namespace!!! change it if it is wrong
// set the driver implementation
$config->setMetadataDriverImpl($driver);
// now create the event manager
$evm = new \Doctrine\Common\EventManager();
$evm->addEventSubscriber(new \Gedmo\Timestampable\TimestampableListener());
$evm->addEventSubscriber(new \Gedmo\Sluggable\SluggableListener());
$translatable = new \Gedmo\Translatable\TranslationListener();
$translatable->setTranslatableLocale('en'); // use your session here, or set it later
$evm->addEventSubscriber($translatable);
// Database connection information
$connectionOptions = array(
'driver' => 'pdo_mysql',
'user' => $db['default']['username'],
'password' => $db['default']['password'],
'host' => $db['default']['hostname'],
'dbname' => $db['default']['database']
);
$em = \Doctrine\ORM\EntityManager::create($connectionOptions, $config, $evm); // you must give EventManager to the constructor!
// thats it. read a doctrine2 tutorial and dont ask me how to create EntityManager or add metadata driver
// because you have overwriten your same driver implementation all over again
$helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(
'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()),
'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em)
));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment