Zend Framework and Doctrine 2 - where do you place your entities?

I am using ZF with Doctrine 2 with a Bisna application resource to link them together. So far, I have put my entities, proxies, repositories, and service levels in my own application folder in libraries. So, for example, my objects will have an App \ Entity namespace. This worked fine, but it seems weird that I don't have all of my entity object in the ZF layout model directory.

So, now I'm trying to move everything to the model directory, and ZF can no longer find my classes. I took care to change the proxy and mapping of namespaces and directories in my application.ini configuration to match the new locations and namespaces (I changed the namespace of the objects to just be Entity, and made the same corresponding changes for the repositories and level service.

In my login controller, I call UserService:

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

I return to a fatal error saying that the class was not found. Any idea what I'm missing here?

Fatal error: Class 'Service \ UserService' not found ...

The namespace of my UserService class is set to Service. This file is located in the /models/Service/UserService.php application.

 namespace Service; class UserService { ... } 

Here are some snippets from my application.ini application 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" 
+4
source share
1 answer

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 have something registering the App namespace, just replace it.

+4
source

All Articles