Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- protected function _initDoctrine() {
- require_once('Doctrine/Common/ClassLoader.php');
- $autoloader = Zend_Loader_Autoloader::getInstance();
- $classLoader = array(new DoctrineCommonClassLoader('Doctrine'), 'loadClass');
- $autoloader->pushAutoloader($classLoader, 'Doctrine\');
- $classLoader = new DoctrineCommonClassLoader('Entities',
- realpath(Zend_Registry::get('config')->resources->entityManager->connection->entities), 'loadClass');
- $autoloader->pushAutoloader(array($classLoader, 'loadClass'), 'Entities');
- $classLoader = new DoctrineCommonClassLoader('Repositories',
- realpath(Zend_Registry::get('config')->resources->entityManager->connection->entities), 'loadClass');
- $autoloader->pushAutoloader(array($classLoader, 'loadClass'), 'Repositories');
- }
- class My_Resource_Entitymanager extends Zend_Application_Resource_ResourceAbstract
- {
- public function init()
- {
- // WARNING: setup, assumes that autoloaders are set
- // configuration settings from the application.ini file
- $zendConfig = new Zend_Config($this->getOptions());
- // globally used cache driver, in production use APC or memcached
- $cache = new DoctrineCommonCacheArrayCache;
- // standard annotation reader
- $annotationReader = new DoctrineCommonAnnotationsAnnotationReader;
- $cachedAnnotationReader = new DoctrineCommonAnnotationsCachedReader(
- $annotationReader, // use reader
- $cache // and a cache driver
- );
- // create a driver chain for metadata reading
- $driverChain = new DoctrineORMMappingDriverDriverChain();
- // load superclass metadata mapping only, into driver chain
- // also registers Gedmo annotations.NOTE: you can personalize it
- GedmoDoctrineExtensions::registerAbstractMappingIntoDriverChainORM(
- $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 DoctrineORMMappingDriverAnnotationDriver(
- $cachedAnnotationReader, // our cached annotation reader
- $zendConfig->connection->entities // 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, 'Entities');
- // general ORM configuration
- $config = new DoctrineORMConfiguration;
- $config->setProxyDir($zendConfig->connection->proxies->location);
- $config->setProxyNamespace($zendConfig->connection->proxies->ns);
- $config->setAutoGenerateProxyClasses($zendConfig->connection->proxies->generate); // this can be based on production config.
- // register metadata driver
- $config->setMetadataDriverImpl($driverChain);
- // use our allready initialized cache driver
- $config->setMetadataCacheImpl($cache);
- $config->setQueryCacheImpl($cache);
- // create event manager and hook prefered extension listeners
- $evm = new DoctrineCommonEventManager();
- // gedmo extension listeners, remove which are not used
- // sluggable
- $sluggableListener = new GedmoSluggableSluggableListener;
- // you should set the used annotation reader to listener, to avoid creating new one for mapping drivers
- $sluggableListener->setAnnotationReader($cachedAnnotationReader);
- $evm->addEventSubscriber($sluggableListener);
- // tree
- $treeListener = new GedmoTreeTreeListener;
- $treeListener->setAnnotationReader($cachedAnnotationReader);
- $evm->addEventSubscriber($treeListener);
- // loggable, not used in example
- $loggableListener = new GedmoLoggableLoggableListener;
- $loggableListener->setAnnotationReader($cachedAnnotationReader);
- $evm->addEventSubscriber($loggableListener);
- // timestampable
- $timestampableListener = new GedmoTimestampableTimestampableListener;
- $timestampableListener->setAnnotationReader($cachedAnnotationReader);
- $evm->addEventSubscriber($timestampableListener);
- // translatable
- $translatableListener = new GedmoTranslatableTranslatableListener;
- // current translation locale should be set from session or hook later into the listener
- // most important, before entity manager is flushed
- $translatableListener->setTranslatableLocale('en');
- $translatableListener->setDefaultLocale('en');
- $translatableListener->setAnnotationReader($cachedAnnotationReader);
- $evm->addEventSubscriber($translatableListener);
- // sortable, not used in example
- $sortableListener = new GedmoSortableSortableListener;
- $sortableListener->setAnnotationReader($cachedAnnotationReader);
- $evm->addEventSubscriber($sortableListener);
- // mysql set names UTF-8 if required
- $evm->addEventSubscriber(new DoctrineDBALEventListenersMysqlSessionInit());
- // DBAL connection
- $connection = array(
- 'driver' => "{$zendConfig->connection->driver}",
- 'host' => "{$zendConfig->connection->host}",
- 'dbname' => "{$zendConfig->connection->dbname}",
- 'user' => "{$zendConfig->connection->user}",
- 'password' => "{$zendConfig->connection->password}"
- );
- // Finally, create entity manager
- $em = DoctrineORMEntityManager::create($connection, $config, $evm);
- Zend_Registry::set('em', $em);
- return $em;
- }
- }
- namespace Entities;
- use DoctrineORMMapping as ORM;
- /**
- * @ORMEntity
- * @ORMHasLifecycleCallbacks
- */
- class USER_User
- {
- /**
- * @ORMId
- * @ORMColumn(type="integer")
- * @ORMGeneratedValue(strategy="AUTO")
- */
- private $id;
- /**
- * Retrieve user id
- */
- public function getId()
- {
- return $this->id;
- }
- }
- ...
- includePaths.library = APPLICATION_PATH "/../library/Doctrine"
- ...
- pluginPaths.My_Resource = "My/Resource"
- ...
- autoloaderNamespaces[] = "Doctrine"
- autoloaderNamespaces[] = "Gedmo"
- autoloaderNamespaces[] = "Symfony"
- autoloaderNamespaces[] = "My"
- ...
- resources.entityManager.connection.driver = "pdo_mysql"
- resources.entityManager.connection.host = "localhost"
- resources.entityManager.connection.dbname = "test"
- resources.entityManager.connection.user = "test"
- resources.entityManager.connection.password = "test"
- resources.entityManager.connection.entities = APPLICATION_PATH "/models"
- resources.entityManager.connection.proxies.location = APPLICATION_PATH "/models/Proxies"
- resources.entityManager.connection.proxies.ns = "Proxies"
- ; According to Doctrine manual, this should be true for
- ; development, and false for production
- resources.entityManager.connection.proxies.generate = true
- ...
- use DoctrineCommonAnnotationsAnnotationRegistry;
- class My_Resource_Entitymanager extends Zend_Application_Resource_ResourceAbstract
- {
- public function init()
- {
- AnnotationRegistry::registerFile(__DIR__ . '/../../Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php');
- // WARNING: setup, assumes that autoloaders are set
- // configuration settings from the application.ini file
- ...
- $classLoader = array(new DoctrineCommonClassLoader('Doctrine'), 'loadClass');
- $autoloader->pushAutoloader($classLoader, 'Doctrine\');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement