Skip to content

Instantly share code, notes, and snippets.

@ericclemmons
Created December 2, 2010 19:42
Show Gist options
  • Save ericclemmons/725925 to your computer and use it in GitHub Desktop.
Save ericclemmons/725925 to your computer and use it in GitHub Desktop.
How to boostrap Doctrine2 ODM (similar with ORM) in Zend Framework
pluginPaths.applicationResources = APPLICATION_PATH "/resources"
resources.doctrine.documentsDir = APPLICATION_PATH "/../library/My/Documents"
resources.doctrine.config.proxyDir = APPLICATION_PATH "/../library/My/Proxies"
resources.doctrine.config.proxyNamespace = "Proxies"
<?php
// Assumes you've installed via PEAR doctrine-common & doctrine orm/odm
use Doctrine\Common\ClassLoader,
Doctrine\Common\Annotations\AnnotationReader,
Doctrine\ODM\MongoDB\DocumentManager,
Doctrine\ODM\MongoDB\Mongo,
Doctrine\ODM\MongoDB\Configuration,
Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver;
class Zend_Resource_Doctrine extends Zend_Application_Resource_ResourceAbstract
{
public function init()
{
$options = $this->getOptions();
$this->registerAutoloaders($options);
// Configuration
$config = new Configuration();
foreach ($options['config'] as $option => $value) {
$method = "set" . ucfirst($option);
$config->{$method}($value);
}
// Document reader
$reader = new AnnotationReader();
$reader->setDefaultAnnotationNamespace('Doctrine\ODM\MongoDB\Mapping\\');
$config->setMetadataDriverImpl(new AnnotationDriver($reader, $options['documentsDir']));
$dm = DocumentManager::create(new Mongo(), $config);
Zend_Registry::set('dm', $dm);
}
public function registerAutoloaders($options)
{
$autoloader = \Zend_Loader_Autoloader::getInstance();
require_once 'Doctrine/Common/ClassLoader.php';
// Doctrine classes
$classLoader = new ClassLoader('Doctrine');
$autoloader->pushAutoloader(array($classLoader, 'loadClass'), 'Doctrine');
// Document classes
$classLoader = new ClassLoader('Documents', $options['documentsDir'] . '/../');
$autoloader->pushAutoloader(array($classLoader, 'loadClass'), 'Documents');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment