【问题标题】:Zend Framework and Doctrine 2 - where do you put your entities?Zend Framework 和 Doctrine 2 - 你把你的实体放在哪里?
【发布时间】:2011-04-27 23:06:58
【问题描述】:

我将 ZF 与 Doctrine 2 与 Bisna 应用程序资源一起使用以将两者联系在一起。到目前为止,我一直将我的实体、代理、存储库和服务层放在我自己的库下的 App 文件夹中。因此,例如,我的实体将具有 App\Entity 的命名空间。这工作正常,但在 ZF 布局的模型目录下没有我的所有实体内容似乎很奇怪。

所以,现在我正试图将所有内容移到模型目录中,而 ZF 无法再找到我的类。我确保在我的 application.ini 配置中更改代理和映射命名空间和目录以匹配新的位置和命名空间(我将我的实体命名空间更改为只是实体,并对存储库和服务层进行了相同的适当更改。

在我的登录控制器中,我调用了 UserService:

$userService = new Service\UserService($em);

我收到一个致命错误,说找不到该类。知道我在这里缺少什么吗?

致命错误:找不到类“Service\UserService”...

我的 UserService 类的命名空间设置为 Service。该文件位于 application/models/Service/UserService.php 中。

namespace Service;

class UserService
{
...
}

以下是我的 application.ini 中的一些片段,显示了我的目录和命名空间

resources.doctrine.orm.entityManagers.default.proxy.namespace = "Proxy"
resources.doctrine.orm.entityManagers.default.proxy.dir = APPLICATION_PATH "/models/Proxy"
resources.doctrine.orm.entityManagers.default.metadataDrivers.0.mappingNamespace = "Entity"
resources.doctrine.orm.entityManagers.default.metadataDrivers.0.mappingDirs[] = APPLICATION_PATH "/models/Entity"

【问题讨论】:

    标签: zend-framework models doctrine-orm


    【解决方案1】:

    您需要将Service 命名空间位置添加到自动加载器。

    在你的 Bootstrap 类中尝试这样的事情

    protected function _initAutoloader()
    {
        $autoloader = Zend_Loader_Autoloader::getInstance();
    
        require_once 'Doctrine/Common/ClassLoader.php';
    
        $serviceAutoloader = new \Doctrine\Common\ClassLoader('Service', APPLICATION_PATH . '/models');
        $autoloader->pushAutoloader(array($serviceAutoloader, 'loadClass'), 'Service');
    
        return $autoloader;
    }
    

    编辑:您可能已经注册了 App 命名空间,只需替换它即可。

    【讨论】:

    • 我正在使用 application.ini 中的 autoloaderNamespaces[] 数组加载 App。我认为这不会起作用。我想我必须像你上面显示的那样做。我试试看。
    • 成功了 - 谢谢!我必须为所有三个命名空间(服务、实体和存储库)添加一个类加载器。最好能解释一下为什么我至少需要 Entity。这应该通过我的教义资源来处理。
    • @Jeremy Doctrine 资源只为 Doctrine 命名空间配置了一个自动加载器
    猜你喜欢
    • 2010-11-14
    • 1970-01-01
    • 1970-01-01
    • 2012-10-14
    • 2011-03-18
    • 2010-09-11
    • 1970-01-01
    • 1970-01-01
    • 2012-11-25
    相关资源
    最近更新 更多