Zend Framework and Doctrine 2 - where do you put your entities?
I'm using ZF with Doctrine 2 with the Bisna application resource to tie the two together. Up until now I've been putting my entities, proxies, repositories, and service layers in my own App folder under libraries. So, for example, my entities would have a namespace of App\Entity. This has worked OK but it seems strange to not have all my entity stuff under the models directory of the ZF layout.
So, now I'm trying to move everything into the models directory and ZF is not able to find my classes any longer. I made sure to change the proxy and mapping namespaces and directories in my application.ini config to match the new locations and namespaces (I changed my entity namespace to just be Entity and made the same appropriate changes for the repositories and service layer.
In my login controller I call the UserService:
$userService = new Service\Us开发者_如何学PythonerService($em);
I get back a fatal error saying the class can't be found. Any idea what I'm missing here?
Fatal error: Class 'Service\UserService' not found...
The namespace on my UserService class is set to Service. This file is sitting in application/models/Service/UserService.php.
namespace Service;
class UserService
{
...
}
Here are some pieces from my application.ini showing my directories and namespaces
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"
You need to add the Service
namespace location to the autoloader.
Try something like this in your Bootstrap class
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;
}
Edit: You probably already had something registering the App
namespace, just replace that.
精彩评论