gpt4 book ai didi

php - Zend Framework 1.11 与 Doctrine 2 和命名空间

转载 作者:搜寻专家 更新时间:2023-10-31 21:41:13 24 4
gpt4 key购买 nike

我一直在我的项目中使用 Doctrine,但没有明确命名我的任何类。这导致了一些问题,试图将我的代码组织到单独的子目录中(或者至少看起来是这样)。因此,我尝试在我的代码中实现 namespace ,但我很挣扎,并且尝试了这里的众多解决方案都无济于事,我需要问一下。

我有标准的项目结构:

application/
--models/
--services/
--controllers/
..etc

在我的 Bootstrap 中,我得到了以下内容(我的代码中没有命名空间但工作正常):

/**
* Initialize Doctrine
* @return Doctrine_Manager
*/
public function _initDoctrine() {
// include and register Doctrine's class loader
require_once('doctrine/Doctrine/Common/ClassLoader.php');

$autoloader = \Zend_Loader_Autoloader::getInstance();

require_once('doctrine/Doctrine/Common/ClassLoader.php');
$commonLoader = new \Doctrine\Common\ClassLoader('Doctrine\Common', 'doctrine');
$autoloader->pushAutoloader(array($commonLoader, 'loadClass'), 'Doctrine\Common');

$dbalLoader = new \Doctrine\Common\ClassLoader('Doctrine\DBAL', 'doctrine');
$autoloader->pushAutoloader(array($dbalLoader, 'loadClass'), 'Doctrine\DBAL');

$ormLoader = new \Doctrine\Common\ClassLoader('Doctrine\ORM', 'doctrine');
$autoloader->pushAutoloader(array($ormLoader, 'loadClass'), 'Doctrine\ORM');

$modelLoader = new \Doctrine\Common\ClassLoader(NULL, APPLICATION_PATH . "/models");
$autoloader->pushAutoloader(array($modelLoader, 'loadClass'), '');

// 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(
APPLICATION_PATH . '/models'
);
$config->setMetadataDriverImpl($driver);

// set the proxy dir and set some options
$config->setProxyDir(APPLICATION_PATH . '/models/Proxies');
$config->setAutoGenerateProxyClasses(true);
//$config->setAutoGenerateProxyClasses(false);
$config->setProxyNamespace('App\Proxies');

// now create the entity manager and use the connection
// settings we defined in our application.ini
$connectionSettings = $this->getOption('doctrine');
$conn = array(
'driver' => $connectionSettings['conn']['driv'],
'user' => $connectionSettings['conn']['user'],
'password' => $connectionSettings['conn']['pass'],
'dbname' => $connectionSettings['conn']['dbname'],
'host' => $connectionSettings['conn']['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;
}

当我添加

namespace models;

到我的每个模型类并更新 Bootstrap 如下我得到一个异常“类应用程序不存在”(应用程序是我的模型之一):

$modelLoader = new \Doctrine\Common\ClassLoader('models', APPLICATION_PATH . "/models");
$autoloader->pushAutoloader(array($modelLoader, 'loadClass'), 'models');

为了完整起见,我在我的 Controller 中引用了该模型,如下所示:

public function indexAction()
{
$this->_helper->layout()->title = "Your applications";
$this->_helper->layout()->description = "Create, edit and view all applications you have registered with us.";
$this->view->applicationList = $this->entityManager->getRepository("Application")->findAll();
}

我错过了什么?我敢肯定这很明显,但我现在真的要拔头发了。

最佳答案

经过进一步搜索,我偶然发现了 this video (需要登录)。

基本上,我解决的方法(根据网络研讨会)是为我的实体命名空间并将它们保存在/library 目录下。然后,每当我需要在实际应用程序中使用它们时,我都会通过它们的命名空间进行访问。

关于php - Zend Framework 1.11 与 Doctrine 2 和命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10861477/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com