Jie*_*eng 8 php doctrine zend-framework namespaces
如果我将Zend Framework 1.10与Doctrine 2集成在一起,我在哪里放置我的Doctrine Models/Entities and Proxies?我想到了/application或者/library目录.如果我确实放入了/library目录,那么它是否会干扰ZF自动加载类,因为那里的类将使用PHP 5.3名称空间和PEAR样式名称空间.
小智 8
我正在开发一个将Doctrine 2与ZF1.10集成在一起的应用程序.你根本不需要使用Doctrine自动加载器.
1)在您的application.ini文件中添加以下行(假设您的库文件夹中安装了Doctrine(与Zend文件夹相同):
autoloadernamespaces.doctrine = "Doctrine"
Run Code Online (Sandbox Code Playgroud)
2)创建一个doctrine或entitymanager资源.在你的ini文件中:
resources.entitymanager.db.driver = "pdo_mysql"
resources.entitymanager.db.user = "user"
resources.entitymanager.db.dbname = "db"
resources.entitymanager.db.host = "localhost"
resources.entitymanager.db.password = "pass"
resources.entitymanager.query.cache = "Doctrine\Common\Cache\ApcCache"
resources.entitymanager.metadata.cache = "Doctrine\Common\Cache\ApcCache"
resources.entitymanager.metadata.driver = "Doctrine\ORM\Mapping\Driver\AnnotationDriver"
resources.entitymanager.metadata.proxyDir = APPLICATION_PATH "/../data/proxies"
resources.entitymanager.metadata.entityDir[] = APPLICATION_PATH "/models/entity"
Run Code Online (Sandbox Code Playgroud)
3)接下来,您需要引导它.我在资源文件夹中添加了一个资源类.确保映射到ini文件中的文件夹:
pluginPaths.Application_Resource_ = APPLICATION_PATH "/resources"
Run Code Online (Sandbox Code Playgroud)
那你的资源类......
class Application_Resource_EntityManager
extends Zend_Application_Resource_ResourceAbstract
{
/**
* @var Doctrine\ORM\EntityManager
*/
protected $_em;
public function init()
{
$this->_em = $this->getEntityManager();
return $this->_em;
}
public function getEntityManager()
{
$options = $this->getOptions();
$config = new \Doctrine\ORM\Configuration();
$config->setProxyDir($options['metadata']['proxyDir']);
$config->setProxyNamespace('Proxy');
$config->setAutoGenerateProxyClasses((APPLICATION_ENV == 'development'));
$driverImpl = $config->newDefaultAnnotationDriver($options['metadata']['entityDir']);
$config->setMetadataDriverImpl($driverImpl);
$cache = new Doctrine\Common\Cache\ArrayCache();
$config->setMetadataCacheImpl($cache);
$config->setQueryCacheImpl($cache);
$evm = new Doctrine\Common\EventManager();
$em = Doctrine\ORM\EntityManager::create($options['db'],$config,$evm);
return $em;
}
}
Run Code Online (Sandbox Code Playgroud)
现在,您的应用程序可以使用doctrine 2实体管理器.在您的控制器中,您可以像这样抓住它:
$bootstrap = $this->getInvokeArg('bootstrap');
$em = $bootstrap->getResource('entitymanager');
Run Code Online (Sandbox Code Playgroud)
我相信这会对某人有所帮助:)