Errors Integrating Doctrine Into Zend Framework
When trying to run a simple command $msg = $this->_em->find("Entities\Message", 1); in my IndexController I get the following errors:
( ! ) Warning: class_parents() [function.class-parents]: Class Entities\Message does not exist and could not be loaded in C:\wamp\www\KosherOven\library\Doctrine\ORM\Mapping\ClassMetadataFactory.php on line 222
Call Stack
# Time Memory Function Location
1 0.0006 372272 {main}( ) ..\index.php:0
2 0.0918 5254808 Zend_Application->run( ) ..\index.php:27
3 0.0918 5254808 Zend_Application_Bootstrap_Bootstrap->run( ) ..\Application.php:366
4 0.0919 5254864 Zend_Controller_Front->dispatch( ) ..\Bootstrap.php:97
5 0.1088 6406328 Zend_Controller_Dispatcher_Standard->dispatch( ) ..\Front.php:954
6 0.1218 6758576 Zend_Controller_Action->dispatch( ) ..\Standard.php:295
7 0.1219 6761896 IndexController->indexAction( ) ..\Action.php:513
8 0.1219 6761992 Doctrine\ORM\EntityManager->find( ) ..\IndexController.php:16
9 0.1225 6764176 Doctrine\ORM\EntityManager->getRepository( ) ..\EntityManager.php:344
10 0.1225 6764240 Doctrine\ORM\EntityManager->getClassMetadata( ) ..\EntityManager.php:567
11 0.1225 6764240 Doctrine\ORM\Mapping\ClassMetadataFactory->getMetadataFor( ) ..\EntityManager.php:251
12 0.1226 6764272 Doctrine\ORM\Mapping\ClassMetadataFactory->loadMetadata( ) ..\ClassMetadataFactory.php:169
13 0.1226 6764456 Doctrine\ORM\Mapping\ClassMetadataFactory->getParentClasses( ) ..\ClassMetadataFactory.php:245
14 0.1226 6764608 class_parents ( ) ..\ClassMetadataFactory.php:222
( ! ) Warning: array_reverse() expects parameter 1 to be array, boolean given in C:\wamp\www\KosherOven\library\Doctrine\ORM\Mapping\ClassMetadataFactory.php on line 222
Call Stack
# Time Memory Function Location
1 0.0006 372272 {main}( ) ..\index.php:0
2 0.0918 5254808 Zend_Application->run( ) ..\index.php:27
3 0.0918 5254808 Zend_Application_Bootstrap_Bootstrap->run( ) ..\Application.php:366
4 0.0919 5254864 Zend_Controller_Front->dispatch( ) ..\Bootstrap.php:97
5 0.1088 6406328 Zend_Controller_Dispatcher_Standard->dispatch( ) ..\Front.php:954
6 0.1218 6758576 Zend_Controller_Action->dispatch( ) ..\Standard.php:295
7 0.1219 6761896 IndexController->indexAction( ) ..\Action.php:513
8 0.1219 6761992 Doctrine\ORM\EntityManager->find( ) ..\IndexController.php:16
9 0.1225 6764176 Doctrine\ORM\EntityManager->getRepository( ) ..\EntityManager.php:344
10 0.1225 6764240 Doctrine\ORM\EntityManager->getClassMetadata( ) ..\EntityManager.php:567
11 0.1225 6764240 Doctrine\ORM\Mapping\ClassMetadataFactory->getMetadataFor( ) ..\EntityManager.php:251
12 0.1226 6764272 Doctrine\ORM\Mapping\ClassMetadataFactory->loadMetadata( ) ..\ClassMetadataFactory.php:169
13 0.1226 6764456 Doctrine\ORM\Mapping\ClassMetadataFactory->getParentClasses( ) ..\ClassMetadataFactory.php:245
14 0.1247 6774560 array_reverse ( ) ..\ClassMetadataFactory.php:222
( ! ) Warning: Invalid argument supplied for foreach() in C:\wamp\www\KosherOven\library\Doctrine\ORM\Mapping\ClassMetadataFactory.php on line 222
Call Stack
# Time Memory Function Location
1 0.0006 372272 {main}( ) ..\index.php:0
2 0.0918 5254808 Zend_Application->run( ) ..\index.php:27
3 0.0918 5254808 Zend_Application_Bootstrap_Bootstrap->run( ) ..\Application.php:366
4 0.0919 5254864 Zend_Controller_Front->dispatch( ) ..\Bootstrap.php:97
5 0.1088 6406328 Zend_Controller_Dispatcher_Standard->dispatch( ) ..\Front.php:954
6 0.1218 6758576 Zend_Controller_Action->dispatch( ) ..\Standard.php:295
7 0.1219 6761896 IndexController->indexAction( ) ..\Action.php:513
8 0.1219 6761992 Doctrine\ORM\EntityManager->find( ) ..\IndexController.php:16
9 0.1225 6764176 Doctrine\ORM\EntityManager->getRepository( ) ..\EntityManager.php:344
10 0.1225 6764240 Doctrine\ORM\EntityManager->getClassMetadata( ) ..\EntityManager.php:56开发者_开发问答7
11 0.1225 6764240 Doctrine\ORM\Mapping\ClassMetadataFactory->getMetadataFor( ) ..\EntityManager.php:251
12 0.1226 6764272 Doctrine\ORM\Mapping\ClassMetadataFactory->loadMetadata( ) ..\ClassMetadataFactory.php:169
13 0.1226 6764456 Doctrine\ORM\Mapping\ClassMetadataFactory->getParentClasses( ) ..\ClassMetadataFactory.php:245
( ! ) Fatal error: Cannot redeclare class Message in C:\wamp\www\KosherOven\application\models\Entities\Message.php on line 9
Call Stack
# Time Memory Function Location
1 0.0006 372272 {main}( ) ..\index.php:0
2 0.0918 5254808 Zend_Application->run( ) ..\index.php:27
3 0.0918 5254808 Zend_Application_Bootstrap_Bootstrap->run( ) ..\Application.php:366
4 0.0919 5254864 Zend_Controller_Front->dispatch( ) ..\Bootstrap.php:97
5 0.1088 6406328 Zend_Controller_Dispatcher_Standard->dispatch( ) ..\Front.php:954
6 0.1218 6758576 Zend_Controller_Action->dispatch( ) ..\Standard.php:295
7 0.1219 6761896 IndexController->indexAction( ) ..\Action.php:513
8 0.1219 6761992 Doctrine\ORM\EntityManager->find( ) ..\IndexController.php:16
9 0.1225 6764176 Doctrine\ORM\EntityManager->getRepository( ) ..\EntityManager.php:344
10 0.1225 6764240 Doctrine\ORM\EntityManager->getClassMetadata( ) ..\EntityManager.php:567
11 0.1225 6764240 Doctrine\ORM\Mapping\ClassMetadataFactory->getMetadataFor( ) ..\EntityManager.php:251
12 0.1226 6764272 Doctrine\ORM\Mapping\ClassMetadataFactory->loadMetadata( ) ..\ClassMetadataFactory.php:169
13 0.1257 6774344 Doctrine\ORM\Mapping\ClassMetadataFactory->newClassMetadataInstance( ) ..\ClassMetadataFactory.php:260
14 0.1301 7172352 Doctrine\ORM\Mapping\ClassMetadata->__construct( ) ..\ClassMetadataFactory.php:351
15 0.1301 7172592 ReflectionClass->__construct( ) ..\ClassMetadata.php:67
16 0.1302 7172896 Doctrine\Common\ClassLoader->loadClass( ) ..\ClassLoader.php:0
17 0.1305 7182920 require( 'C:\wamp\www\KosherOven\application\models\Entities\Message.php' ) ..\ClassLoader.php:148
Here is my doctrine bootstrap code:
protected function _initDoctrine()
{
/* Autoload classes */
require_once 'Doctrine/Common/ClassLoader.php';
$classLoader = new Doctrine\Common\ClassLoader('Doctrine');
$classLoader->register();
// load the doctrine class loader for Symfony library components
$classLoader = new \Doctrine\Common\ClassLoader('Symfony', 'Doctrine');
$classLoader->register();
$classLoader = new \Doctrine\Common\ClassLoader('Entities', APPLICATION_PATH . '/models');
$classLoader->register();
/* Choose cache */
if (APPLICATION_ENV == "development") {
$cache = new \Doctrine\Common\Cache\ArrayCache;
} else {
//$cache = new \Doctrine\Common\Cache\ApcCache;
$cache = new \Doctrine\Common\Cache\ArrayCache;
}
$config = new \Doctrine\ORM\Configuration;
$config->setMetadataCacheImpl($cache);
$driverImpl = $config->newDefaultAnnotationDriver(APPLICATION_PATH . '/configs/doctrine/Mapping');
$config->setMetadataDriverImpl($driverImpl);
$config->setQueryCacheImpl($cache);
$config->setProxyDir(APPLICATION_PATH . '/Proxies');
$config->setProxyNamespace('Proxies');
if (APPLICATION_ENV == "development") {
$config->setAutoGenerateProxyClasses(true);
} else {
$config->setAutoGenerateProxyClasses(false);
}
$configObj = $this->_initConfig();
$connectionOptions = array(
'driver' => $configObj->database->adapter,
'dbname' => $configObj->database->params->dbname,
'host' => $configObj->database->params->host,
'user' => $configObj->database->params->username,
'password' => $configObj->database->params->password
);
$em = \Doctrine\ORM\EntityManager::create($connectionOptions, $config);
Zend_Registry::set('em', $em);
}
I am using Doctrine 2.0.4.
I can't think of any other info to provide, but I would be happy to add more information if needed.
Make sure you have the following class in application/models/Entities/Message.php
namespace Entities;
/**
* @Entity
*/
class Message
{
// ...
Also, I'm not sure about this line
// load the doctrine class loader for Symfony library components
$classLoader = new \Doctrine\Common\ClassLoader('Symfony', 'Doctrine');
Isn't that saying the Symfony
namespace / folder is in the Doctrine
directory?
Edit: My guess would be it should look like this
$classLoader = new \Doctrine\Common\ClassLoader('Symfony');
UPDATE
I think I see what's missing. You aren't telling the annotation driver where to find your entities.
I think this line
$driverImpl = $config->newDefaultAnnotationDriver(APPLICATION_PATH . '/configs/doctrine/Mapping');
should be
$driverImpl = $config->newDefaultAnnotationDriver(APPLICATION_PATH . '/models/Entities');
See http://www.doctrine-project.org/docs/orm/2.0/en/reference/configuration.html
I highly recommend you check out the Bisna integration from Guilherme Blanco - https://github.com/guilhermeblanco/ZendFramework1-Doctrine2
I also have a sort of ZF/Doctrine2 app skeleton here that you're welcome to look at - https://github.com/philBrown/ZFDoctrineSkeleton
Looks like you're not pushing your Doctrine, Symfony, and Entities class loaders onto the Zend autoloader:
require_once 'Doctrine/Common/ClassLoader.php';
$autoloader = \Zend_Loader_Autoloader::getInstance();
$symfonyAutoloader = new \Doctrine\Common\ClassLoader('Symfony');
$autoloader->pushAutoloader(array($symfonyAutoloader, 'loadClass'), 'Symfony');
$doctrineAutoloader = new \Doctrine\Common\ClassLoader('Doctrine');
$autoloader->pushAutoloader(array($doctrineAutoloader, 'loadClass'), 'Doctrine');
$entityAutoloader = new \Doctrine\Common\ClassLoader('Entities', APPLICATION_PATH . '/models');
$autoloader->pushAutoloader(array($entityAutoloader, 'loadClass'), 'Entities');
精彩评论