Skip to content

Instantly share code, notes, and snippets.

@eminetto
Created May 10, 2015 23:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eminetto/e66bd041328eadd87db5 to your computer and use it in GitHub Desktop.
Save eminetto/e66bd041328eadd87db5 to your computer and use it in GitHub Desktop.
bootstrap.php
<?php
//AutoLoader do Composer
$loader = require __DIR__.'/vendor/autoload.php';
//vamos adicionar nossas classes ao AutoLoader
$loader->add('DoctrineNaPratica', __DIR__.'/src');
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
//se for falso usa o APC como cache, se for true usa cache em arrays
$isDevMode = true;
//caminho das entidades
$paths = array(__DIR__ . '/src/DoctrineNaPratica/Model');
// configurações do banco de dados
$dbParams = array(
'driver' => 'pdo_mysql',
'user' => 'dnp',
'password' => 'dnp',
'dbname' => 'dnp',
'host' => '127.0.0.1',
'port' => 3306
);
$config = Setup::createConfiguration($isDevMode);
//leitor das annotations das entidades
$driver = new AnnotationDriver(new AnnotationReader(), $paths);
$config->setMetadataDriverImpl($driver);
//registra as annotations do Doctrine
AnnotationRegistry::registerFile(
__DIR__ . '/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php'
);
// Enable second-level-cache
$cache = new \Doctrine\Common\Cache\ApcCache;
$cacheRegionConfiguration = new \Doctrine\ORM\Cache\RegionsConfiguration();
$factory = new \Doctrine\ORM\Cache\DefaultCacheFactory($cacheRegionConfiguration, $cache);
$config->setSecondLevelCacheEnabled();
$config->getSecondLevelCacheConfiguration()->setCacheFactory($factory);
//cria o entityManager
$entityManager = EntityManager::create($dbParams, $config);
//subscriber
$evm = $entityManager->getEventManager();
$entitySubscriber = new DoctrineNaPratica\Model\Subscriber\EntitySubscriber;
$evm->addEventSubscriber($entitySubscriber);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment