Created
December 2, 2010 19:42
-
-
Save ericclemmons/725925 to your computer and use it in GitHub Desktop.
How to boostrap Doctrine2 ODM (similar with ORM) in Zend Framework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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