Integrating Doctrine in Zend framework

Posted by | February 22, 2012 | PHP, Zend | No Comments

Zend is, in my opinion, the best PHP framework ever it has a lot of really nice useful stuff, but it doesn’t have a build in ORM. I have recently worked with Doctrine ORM 2.0 that is a superb ORM framework, wrote on PHP 5.3 can be easily integrated in Zend.
So, lets keep it short,
1. download and copy the doctrine framework to the library folder
2. add the following lines to the applicaion.ini:

doctrine.connection.host = 'mysql db host'
doctrine.connection.user = 'mysql user'
doctrine.connection.pass = 'mysql pass'
doctrine.connection.driv = 'pdo_mysql'
doctrine.connection.dbname = 'mysql databse'
doctrine.path.models = APPLICATION_PATH "/models/"
doctrine.proxyNamespace='\Proxies'
doctrine.proxyPath=APPLICATION_PATH . '/models/Proxies'
doctrine.modelsPath= APPLICATION_PATH . '/models'


2. add some glue code in Bootstrap.php

/**
* Initialize Doctrine
* @return Doctrine_Manager
*/
public function _initDoctrine() {
 
// include and register Doctrine's class loader
require_once('Doctrine/Common/ClassLoader.php');
$classLoader = new \Doctrine\Common\ClassLoader(
'Doctrine',
APPLICATION_PATH . '/../library/'
);
 
// now create the entity manager and use the connection
// settings we defined in our application.ini
$doctrineSettings = $this->getOption('doctrine');
 
$classLoader->register();
 
// create the Doctrine configuration
$config = new \Doctrine\ORM\Configuration();
 
// setting the cache ( to ArrayCache. Take a look at
// the Doctrine manual for different options ! )
$cache = new \Doctrine\Common\Cache\ArrayCache;
$config->setMetadataCacheImpl($cache);
$config->setQueryCacheImpl($cache);
 
// choosing the driver for our database schema
// we'll use annotations
$driver = $config->newDefaultAnnotationDriver(
$doctrineSettings['modelsPath']
);
$config->setMetadataDriverImpl($driver);
// set the proxy dir and set some options
$config->setProxyDir($doctrineSettings['proxyPath']);
$config->setAutoGenerateProxyClasses(true);
$config->setProxyNamespace($doctrineSettings['proxyNamespace']);
 
$conn = array(
'driver' => $doctrineSettings['connection']['driv'],
'user' => $doctrineSettings['connection']['user'],
'password' => $doctrineSettings['connection']['pass'],
'dbname' => $doctrineSettings['connection']['dbname'],
'host' => $doctrineSettings['connection']['host']
);
$entityManager = \Doctrine\ORM\EntityManager::create($conn, $config);
 
// push the entity manager into our registry for later use
$registry = Zend_Registry::getInstance();
$registry->entitymanager = $entityManager;
 
return $entityManager;
}

So this is it, you have successfully added doctrine to your Zend project.

About Andrei Tara

Andrei is a developer/geek who loves creating high quality and efficient applications, always following the best practices in software engineering. He loves the challenge of learning new, exciting things and playing with new languages, frameworks, and tools.

Leave a Reply