如何开始使用Doctrine 2 + ZF?任何教程或资源?
在附注中,我听说ZF2将使用Doctrine作为其模型,是吗?
答案 0 :(得分:3)
使用ZF和Doctrine 2的好处是,要集成它们几乎没有什么需要做的。基本上,您只需要访问在应用程序引导期间设置的Doctrine 2 EntityManager
实例,并确保在index.php
中加载Doctrine命名空间(您需要使用) Doctrine的ClassLoader,Zend_Loader
还不支持名称空间。)
您可以通过Resource Plugin在引导程序中手动实例化EntityManager
,或者更容易实例化(这使得在application.ini
中存储数据库配置变得容易)。你几乎可以按照Doctrine手册中configuring and obtaining an Entity Manager的文档进行操作,只需让你的bootstrap资源中的init()
方法返回实例。
您可能希望高度依赖依赖注入来将EM传递给需要它的各种对象。有关将引导程序资源传递到操作控制器的简便方法,请参阅creating a simple resource injector上的这篇文章。
我一直在使用带有ZF的Doctrine 2,因为它已经使用了alpha版本,并且发现它非常令人愉快。
答案 1 :(得分:2)
我可以从我使用的bootstrap.php中给你一些例子:
public function _initDoctrine() {
if(PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 3) {
require_once('bootstrapDoctrine.inc.php');
//things below this line are for convenience
require_once(dirname(__FILE__).'/../library/doctrinehelpers/requireEntitiesOnce.php');
Zend_Registry::set('doctrineEm', $em);
return $em;
}
}
在boostrapDoctrine.inc.php中我有这个:
use Doctrine\Common\ClassLoader,
Doctrine\ORM\Configuration,
Doctrine\ORM\EntityManager,
Doctrine\Common\Cache\ArrayCache,
Doctrine\DBAL\Logging\EchoSQLLogger;
require_once(realpath(APPLICATION_PATH . '/../library').'/doctrine2/lib/Doctrine/Common/ClassLoader.php');
$doctrineClassLoader = new ClassLoader('Doctrine', realpath(APPLICATION_PATH . '/../library').'/doctrine2/lib');
$doctrineClassLoader->register();
//no way to have your proxies generated in different directory per ZF module it seems so we use a global one
$proxiesClassLoader = new ClassLoader('Proxies', realpath(APPLICATION_PATH . '/models/doctrineproxies'));
$proxiesClassLoader->register();
/*
* @TODO make this step iterate over available modules
*/
$driverImpl = $config->newDefaultAnnotationDriver(array(APPLICATION_PATH . '/modules/mymodule1/models/doctrineentities',APPLICATION_PATH . '/modules/mymodule2/models/doctrineentities'));
$config->setMetadataDriverImpl($driverImpl);
$config->setMetadataCacheImpl($cache);
$config->setQueryCacheImpl($cache);
// Proxy configuration
$config->setProxyDir(realpath(APPLICATION_PATH . '/models/doctrineproxies'));
$config->setProxyNamespace('Proxies');
/**
* this SQL logger is golden
* @TODO implement a switch for verbose debugging
*/
// $logger = new Doctrine\DBAL\Logging\DebugStack();
// $config->setSQLLogger($logger);
// register_shutdown_function(function($logger) {
// echo '<pre>';
// print_r($logger->queries);
// }, $logger);
$config->setAutoGenerateProxyClasses( true ); //disable in production environment
$doctrineConfig = $this->getOption('resources'); //from ini
$dbparams = $doctrineConfig['db']['params'];
$connectionOptions = array(
'driver' => $doctrineConfig['db']['adapter'],
'user' => $dbparams['username'],
'password' => $dbparams['password'],
'dbname' => $dbparams['dbname'],
'host' => $dbparams['host']
);
$em = EntityManager::create($connectionOptions, $config); //stored in zend registry later
要允许doctrine命令行工具运行,我必须创建一个库/ doctrine2 / lib / cli-config.php,它也是一个精简的zend框架引导程序。这个配置的缺点是我必须从这个目录中调用doctrine cli。适合我;)
/*
* @TODO make the cli-tool more flexible by better path detection
*/
// Define path to application directory
defined('APPLICATION_PATH') || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../../../application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
realpath(APPLICATION_PATH . '/../../include'),
get_include_path(),
)));
/** Zend_Application */
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$ application->getBootstrap()->bootstrap('doctrine');
$em = $application->getBootstrap()->getResource('doctrine');
/*
$configuration = new \Doctrine\Common\Cli\Configuration();
$configuration->setAttribute('em', $em);
*/
$helperSet = new \Symfony\Components\Console\Helper\HelperSet(array(
'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()),
'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em)
));
现在我们都希望有一个更好的学说整合,但这只会发生在ZF 2中,朝着像学说这样的命名空间迈出了一大步。
希望我能提供帮助。