Skip to content

Instantly share code, notes, and snippets.

@memphys
Created February 27, 2011 21:33
Show Gist options
  • Save memphys/846562 to your computer and use it in GitHub Desktop.
Save memphys/846562 to your computer and use it in GitHub Desktop.
Doctrine2 bootstrap in zf1
[production]
includePaths.library = APPLICATION_PATH "/../library"
;; DB
db.driver = "pdo_mysql"
db.host = "localhost"
db.dbname = "dbname"
db.user = "dbuser"
db.password = "somePassword"
;; Cache
cache.frontendOptions.lifetime = 86400
cache.frontendOptions.automatic_serialization = true
cache.frontendOptions.cache_id_prefix = prfx
cache.backend = Memcached
cache.backendOptions.servers.host = 127.0.0.1
cache.backendOptions.servers.port = 11211
cache.backendOptions.servers.persistent = true
cache.backendOptions.compression = false
[development : production]
;; DB
db.user = "root"
db.password = ""
<?php
protected function _initDoctrine()
{
$options = $this->getOptions();
$doctrinePath = $options['includePaths']['library'];
require_once $doctrinePath . '/Doctrine/Common/ClassLoader.php';
$autoloader = Zend_Loader_Autoloader::getInstance();
$doctrineAutoloader = array(new \Doctrine\Common\ClassLoader(), 'loadClass');
$autoloader->pushAutoloader($doctrineAutoloader, 'Doctrine');
$classLoader = new \Doctrine\Common\ClassLoader('Entities', realpath(__DIR__ . '/models/'), 'loadClass');
$autoloader->pushAutoloader(array($classLoader, 'loadClass'), 'Entities');
$classLoader = new \Doctrine\Common\ClassLoader('Symfony', realpath(__DIR__ . '/../library/Doctrine/'), 'loadClass');
$autoloader->pushAutoloader(array($classLoader, 'loadClass'), 'Symfony');
if (APPLICATION_ENV == "development") {
$cache = new \Doctrine\Common\Cache\ArrayCache();
} else {
$cacheOptions = $options['cache']['backendOptions'];
$cache = new \Doctrine\Common\Cache\MemcacheCache();
$memcache = new Memcache;
$memcache->connect($cacheOptions['servers']['host'], $cacheOptions['servers']['port']);
$cache->setMemcache($memcache);
}
$config = new \Doctrine\ORM\Configuration();
$config->setMetadataCacheImpl($cache);
$driverImpl = $config->newDefaultAnnotationDriver(APPLICATION_PATH . '/models/Entities');
$config->setMetadataDriverImpl($driverImpl);
$config->setQueryCacheImpl($cache);
$config->setProxyDir(APPLICATION_PATH . '/models/Proxies');
$config->setProxyNamespace('Proxies');
if (APPLICATION_ENV == "development") {
$config->setAutoGenerateProxyClasses(true);
} else {
$config->setAutoGenerateProxyClasses(false);
}
$em = \Doctrine\ORM\EntityManager::create($options['db'], $config);
Zend_Registry::set('em', $em);
return $em;
}
<?php
class My_Controller_Action_Helper_Em extends Zend_Controller_Action_Helper_Abstract
{
/**
* Entity manager
* @var \Doctrine\ORM\EntityManager
*/
private $_em = null;
/**
* Return entity manager
* Could be overriden to support custom entity manager
*
* @return \Doctrine\ORM\EntityManager
*/
public function getEntityManager()
{
if ( $this->_em == null) {
$this->_em = Zend_Registry::get('em');
}
return $this->_em;
}
/**
* @return \Doctrine\ORM\EntityManager
*/
public function direct()
{
return $this->getEntityManager();
}
}
<?php
class Default_IndexController extends Zend_Controller_Action
{
public function indexAction()
{
$user = new \Entities\User();
$user->name = 'test';
$user->login = 'test';
$user->password = md5('123456');
$em = $this->_helper->Em();
$em->persist($user);
$em->flush();
}
}
<?php
namespace Entities;
/**
* @Entity
* @Table(name="users")
*/
class User
{
/** @Id @Column(type="integer") @GeneratedValue */
public $id;
/** @Column(length=50,nullable=true) */
public $name;
/** @Column(length=50) */
public $login;
/** @Column(length=32) */
public $password;
/** @Column(type="integer") */
public $role;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment