Skip to content

Instantly share code, notes, and snippets.

@return1
Created November 7, 2012 16:24
Show Gist options
  • Save return1/4032609 to your computer and use it in GitHub Desktop.
Save return1/4032609 to your computer and use it in GitHub Desktop.
Integration of Doctrine2 and Zend Framework
/**
* Register namespace Application_
* @return Zend_Application_Module_Autoloader
*/
protected function _initAutoload() {
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'Application',
'basePath' => dirname(__FILE__),
));
return $autoloader;
}
/**
* Initialize auto loader of Doctrine
*
* @return Doctrine_Manager
*/
protected function _initDoctrine() {
// Fetch the global Zend Autoloader
$autoloader = Zend_Loader_Autoloader::getInstance();
$lib = '/path/to/Doctrine'; //path to git repository of Doctrine2 on filesystem
require_once($lib.'/vendor/doctrine-common/lib/Doctrine/Common/ClassLoader.php');
//doctrine autoloader
$classLoader = new \Doctrine\Common\ClassLoader('Doctrine\Common', $lib.'/vendor/doctrine-common/lib');
$classLoader->register();
$classLoader = new \Doctrine\Common\ClassLoader('Doctrine\DBAL', $lib.'/vendor/doctrine-dbal/lib');
$classLoader->register();
$classLoader = new \Doctrine\Common\ClassLoader('Doctrine\ORM', $lib);
$classLoader->register();
//Push the doctrine autoloader to load for the Doctrine\ namespace
$autoloader->pushAutoloader($classLoader, 'Doctrine\\');
//init arraycache
$cache = new \Doctrine\Common\Cache\ArrayCache;
//setup configuration as seen from the sandbox application from the doctrine2 docs
//http://www.doctrine-project.org/documentation/manual/2_0/en/configuration
$config = new \Doctrine\ORM\Configuration();
$config->setMetadataCacheImpl($cache);
$driverImpl = $config->newDefaultAnnotationDriver(APPLICATION_PATH . '/../doctrine/entities');
$config->setMetadataDriverImpl($driverImpl);
$config->setQueryCacheImpl($cache);
$config->setProxyDir(APPLICATION_PATH . '/../doctrine/proxies');
$config->setProxyNamespace('Application\Proxies');
$config->setAutoGenerateProxyClasses(true);
//therefore you need some entries in your config:
//doctrine.conn.host = 'localhost'
//doctrine.conn.user = 'someuser'
//doctrine.conn.pass = 'somepwd'
//doctrine.conn.driv = 'pdo_pgsql' //i use postgres
//doctrine.conn.dbname = 'somedbname'
$doctrineConfig = $this->getOption('doctrine');
$connectionOptions = array(
'driver' => $doctrineConfig['conn']['driv'],
'user' => $doctrineConfig['conn']['user'],
'password' => $doctrineConfig['conn']['pass'],
'dbname' => $doctrineConfig['conn']['dbname'],
'host' => $doctrineConfig['conn']['host']
);
$em = \Doctrine\ORM\EntityManager::create($connectionOptions, $config);
Zend_Registry::set('entitymanager', $em);
return $em;
}
class IndexController extends Zend_Controller_Action {
public function init() {
$this->_em = Zend_Registry::get('entitymanager');
}
public function indexAction() {
$test = new Application_Model_Test();
$test->name = 'Test';
$this->_em->persist($test);
$this->_em->flush();
}
}
/**
* @Entity
* @Table(name="test")
*/
class Application_Model_Test {
/**
* @Id @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
*/
private $id;
/** @Column(type="string") */
public $name;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment