Skip to content
123 lines (106 sloc) 4.9 KB
* This entity manager configuration works with doctrine 2.1.x and 2.2.x
* versions. Regarding AnnotationDriver setup it most probably will be changed into
* xml. Because annotation driver fails to read other classes in same namespace
// connection args, modify at will
$connection = array(
'host' => '',
'port' => 3306,
'user' => 'root',
'password' => 'nimda',
'dbname' => 'test',
'driver' => 'pdo_mysql',
if (!file_exists(__DIR__.'/../vendor/autoload.php')) {
die('cannot find vendors, read how to use composer');
// First of all autoloading of vendors
$loader = require __DIR__.'/../vendor/autoload.php';
// gedmo extensions
$loader->add('Gedmo', __DIR__.'/../lib');
// autoloader for Entity namespace
$loader->add('Entity', __DIR__.'/app');
// ensure standard doctrine annotations are registered
// Second configure ORM
// globally used cache driver, in production use APC or memcached
$cache = new Doctrine\Common\Cache\ArrayCache();
// standard annotation reader
$annotationReader = new Doctrine\Common\Annotations\AnnotationReader();
$cachedAnnotationReader = new Doctrine\Common\Annotations\CachedReader(
$annotationReader, // use reader
$cache // and a cache driver
// create a driver chain for metadata reading
$driverChain = new Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain();
// load superclass metadata mapping only, into driver chain
// also registers Gedmo annotations.NOTE: you can personalize it
$driverChain, // our metadata driver chain, to hook into
$cachedAnnotationReader // our cached annotation reader
// now we want to register our application entities,
// for that we need another metadata driver used for Entity namespace
$annotationDriver = new Doctrine\ORM\Mapping\Driver\AnnotationDriver(
$cachedAnnotationReader, // our cached annotation reader
array(__DIR__.'/app/Entity') // paths to look in
// NOTE: driver for application Entity can be different, Yaml, Xml or whatever
// register annotation driver for our application Entity namespace
$driverChain->addDriver($annotationDriver, 'Entity');
// general ORM configuration
$config = new Doctrine\ORM\Configuration();
$config->setAutoGenerateProxyClasses(false); // this can be based on production config.
// register metadata driver
// use our allready initialized cache driver
// Third, create event manager and hook prefered extension listeners
$evm = new Doctrine\Common\EventManager();
// gedmo extension listeners
// sluggable
$sluggableListener = new Gedmo\Sluggable\SluggableListener();
// you should set the used annotation reader to listener, to avoid creating new one for mapping drivers
// tree
$treeListener = new Gedmo\Tree\TreeListener();
// loggable, not used in example
//$loggableListener = new Gedmo\Loggable\LoggableListener;
// timestampable
$timestampableListener = new Gedmo\Timestampable\TimestampableListener();
// blameable
$blameableListener = new \Gedmo\Blameable\BlameableListener();
$blameableListener->setUserValue('MyUsername'); // determine from your environment
// translatable
$translatableListener = new Gedmo\Translatable\TranslatableListener();
// current translation locale should be set from session or hook later into the listener
// most important, before entity manager is flushed
// sortable, not used in example
//$sortableListener = new Gedmo\Sortable\SortableListener;
// mysql set names UTF-8 if required
$evm->addEventSubscriber(new Doctrine\DBAL\Event\Listeners\MysqlSessionInit());
// Finally, create entity manager
return Doctrine\ORM\EntityManager::create($connection, $config, $evm);
Jump to Line
Something went wrong with that request. Please try again.