Skip to content

Instantly share code, notes, and snippets.

@Hounddog
Created August 3, 2012 13:53
Show Gist options
  • Save Hounddog/3247906 to your computer and use it in GitHub Desktop.
Save Hounddog/3247906 to your computer and use it in GitHub Desktop.
Zf1 Doctrine Configuration
resources.db.adapter = PDO_MYSQL
resources.db.params.host = localhost
resources.db.params.username = username
resources.db.params.password = password
resources.db.params.dbname = database.name
resources.db.params.charset = utf8
resources.db.isDefaultTableAdapter = true
pluginPaths.Resource = "Path/to/Resource"
resources.entityManager.proxyDir = APPLICATION_PATH "/proxies"
resources.entityManager.proxyNamespace = Proxies
resources.entityManager.autoGenerateProxyClasses = false
resources.entityManager.cache = \Doctrine\Common\Cache\ArrayCache
resources.entityManager.sqlLog = false
<?php
// Display errors ?
//ini_set('display_errors', 1);
//error_reporting(E_ALL | E_STRICT);
// Define path to application directory
// DO NOT FORGET TO EDIT
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH','/path/to/your/application');
// Define application environment
defined('APPLICATION_ENV')
|| define(
'APPLICATION_ENV',
(getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'development')
);
// Ensure library/ is on include_path
// DO NOT FORGET TO EDIT
set_include_path(
'/path/to/where/your/zend/library/is' . PATH_SEPARATOR .
'/path/to/Doctrine' . PATH_SEPARATOR .
get_include_path()
);
// Requiring a batch of Classes we will need for namespacing
use Doctrine\Common\ClassLoader,
Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper,
Doctrine\ORM\Version,
Doctrine\ORM\Tools\Console\ConsoleRunner,
Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper,
Symfony\Component\Console\Helper\HelperSet,
Symfony\Component\Console\Helper\DialogHelper,
Symfony\Component\Console\Application;
// We need the Doctrine ClassLoader to manage autoloading
require_once 'Doctrine/Common/ClassLoader.php';
// Load Doctrine
$classLoader = new ClassLoader('Doctrine');
$classLoader->register();
// Load Symfony tools
$classLoader = new ClassLoader('Symfony', 'Doctrine');
$classLoader->register();
// Zend_Application
require_once 'Zend/Application.php';
// Create application
// DO NOT FORGET TO EDIT
$application = new Zend_Application(
APPLICATION_ENV, '/path/to/application.ini'
);
// Bootstrap
$application->bootstrap();
// loading doctrine resource, sometimes called entityManager
// DO NOT FORGET TO EDIT, set the resource to your entityManager
$em = $application->getBootstrap()->getResource('db');
// Load doctrine helpers
$helperSet = new HelperSet(array(
'db' => new ConnectionHelper($em->getConnection()),
'em' => new EntityManagerHelper($em),
'dialog' => new DialogHelper()
));
$cli = new Application('Doctrine Command Line Interface', Version::VERSION);
$cli->setCatchExceptions(true);
$cli->setHelperSet($helperSet);
// We are settign the commands to bypass the configuration process and
// directly use our ZendConfiguration
// Just make sure to load from where the file actually is
require_once('ZendConfiguration.php');
$connexion = $em->getConnection();
$zendConfig = new ZendConfiguration($connexion);
// injecting configurations necessary to our ZendConfiguration
// Pass the Application.ini Parameters to our Configuration
$applicationConfig = new Zend_Config(
$application->getBootstrap()->getOptions(), true
);
// DO NOT FORGET TO EDIT, if needed
$zendConfig->setConfig($applicationConfig->resources->db->migration);
// Here we just need to put some string because the parameter is not optional
// though will not need it. Just need to call the function
$zendConfig->load('zend');
// Register All Doctrine Commands
ConsoleRunner::addCommands($cli);
// Runs console application
$cli->run();
<?php
use Doctrine\ORM\EntityManager,
Doctrine\ORM\Configuration,
Doctrine\DBAL\Event\Listeners\MysqlSessionInit,
Doctrine\Common\Cache\ArrayCache,
Doctrine\Common\Annotations\AnnotationRegistry,
Doctrine\Common\Annotations\AnnotationReader,
Doctrine\Common\Annotations\CachedReader,
Doctrine\Common\Annotations\IndexedReader,
Doctrine\ORM\Mapping\Driver\Driver,
Doctrine\ORM\Mapping\Driver\AnnotationDriver;
class Resource_Entitymanager extends
Zend_Application_Resource_ResourceAbstract
{
protected $_options = array();
public function init()
{
$options = $this->getOptions();
$config = new Configuration;
$config->setProxyDir($options['proxyDir']);
$config->setProxyNamespace($options['proxyNamespace']);
// config annotations
AnnotationRegistry::registerFile(
'Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php'
);
$reader = new CachedReader(
new IndexedReader(new AnnotationReader()),
new ArrayCache()
);
$driverImpl = new AnnotationDriver(
$reader,
array(APPLICATION_PATH . '/modules')
);
// application own annotations
// TODO : namespace it when the application will use namespace
AnnotationReader::addGlobalIgnoredName('service');
AnnotationReader::addGlobalIgnoredName('transport');
AnnotationReader::addGlobalIgnoredName('envelope');
AnnotationReader::addGlobalIgnoredName('contentType');
AnnotationReader::addGlobalIgnoredName('dto');
$config->setMetadataDriverImpl($driverImpl);
$config->setAutoGenerateProxyClasses(
$options['autoGenerateProxyClasses']
);
//Caching
if(Zend_Registry::get('cacheEnabled')) {
$cacheDriver = new \Doctrine\Common\Cache\ApcCache();
$config->setQueryCacheImpl($cacheDriver);
$config->setMetadataCacheImpl($cacheDriver);
$config->setResultCacheImpl($cacheDriver);
}
$configDb = $this->getBootstrap()->getResource('db')->getConfig();
$entityManager = EntityManager::create(
array(
'driver' => 'pdo_mysql',
'host' => $configDb['host'],
'dbname' => $configDb['dbname'],
'user' => $configDb['username'],
'password' => $configDb['password']
),
$config
);
$entityManager->getEventManager()->addEventSubscriber(
new MysqlSessionInit($configDb['charset'])
);
if (isset($options['sqlLog']) && $options['sqlLog']) {
$logger = new Pwb_Resource_SqlLogger;
$logger->setConnection($entityManager->getConnection());
$config->setSQLLogger($logger);
}
Zend_Registry::set('entityManager', $entityManager);
return $entityManager;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment