Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine not working unless I explicitly call AnnotationDriver::getAllClassNames

I'm attempting to use Doctrine ORM for the first time, and I'm following the configuration found here.

Following these steps directly results in the following error:

Warning: class_parents() [function.class-parents]: Class MyProject\Model\User does not exist and could not be loaded in /opt/local/lib/php/Doctrine/ORM/Mapping/ClassMetadataFactory.php on line 222

Warning: array_reverse() expects parameter 1 to be array, boolean given in /opt/local/lib/php/Doctrine/ORM/Mapping/ClassMetadataFactory.php on line 222

Warning: Invalid argument supplied for foreach() in /opt/local/lib/php/Doctrine/ORM/Mapping/ClassMetadataFactory.php on line 222

Fatal error: Uncaught exception 'ReflectionException' with message 'Class MyProject\Model\User does not exist' in /opt/local/lib/php/Doctrine/ORM/Mapping/ClassMetadata.php:67

Stack trace:

#0 /opt/local/lib/php/Doctrine/ORM/Mapping/ClassMetadata.php(67): ReflectionClass->__construct('MyProject\Model...')

#1 /opt/local/lib/php/Doctrine/ORM/Mapping/ClassMetadataFactory.php(350): Doctrine\ORM\Mapping\ClassMetadata->__construct('MyProject\Model...')

#2 /opt/local/lib/php/Doctrine/ORM/Mapping/ClassMetadataFactory.php(260): Doctrine\ORM\Mapping\ClassMetadataFactory->newClassMetadataInstance('MyProject\Model...')

#3 /opt/local/lib/php/Doctrine/ORM/Mapping/ClassMetadataFactory.php(169): Doctrine\ORM\Mapping\ClassMetadataFactory->loadMetadata('MyProject\Model...')

#4 /opt/local/lib/php/Doctrine/ORM/EntityManager.php(247): Doctrine\ORM\Mapping\ClassMetadataFactory->getMetadataFor('MyProject\Model...')

#5 /opt/local/lib/php/Doctrine/ORM/EntityManager.php(563): Doctrine\ORM\EntityManager->getClassMetadata('MyProject\Model...')

#6 /opt in /opt/local/lib/php/Doctrine/ORM/Mapping/ClassMetadata.php on line 67

This error goes away, and the code works perfectly if in the sample code, I add the following line (denoted with the comment below):

$driverImpl = $config->newDefaultAnnotationDriver('/path/to/lib/MyProject/Entities');
$driverImpl->getAllClassNames();  // **MY ADDED LINE**
$config->setMetadataDriverImpl($driverImpl);

There is nothing that mentions this call being required to get the sample code to work; it was just something I happened to stumble across while echoing some info to the screen while trying to see where my error is occurring.

Is there a reason this call is needed (and why it's not mentioned in the sample code)? Is there something else that should instead be called, which is the root cause to my original error?

(I can post more code as required, I'm just not positive what is most helpful in soliving this since it's my first time using Doctrine.)

EDIT: I neglected to mention the line on which the error actually occurs:

$user = $em->find('MyProject\Model\User', 1);
like image 417
Matt Huggins Avatar asked Dec 26 '10 00:12

Matt Huggins


1 Answers

It sounds like you do not have a class loader set up for your project correctly:

$classloader = new \Doctrine\Common\ClassLoader('MyProject', '/path/to/lib/');
$classloader->register();

The reason calling getAllClassNames() is fixing your issue is because it traverses all of the directories and files inside of /path/to/lib/MyProject/Entities and includes them. Obviously this is slow and isn't recommended.

like image 183
Michael Ridgway Avatar answered Oct 04 '22 07:10

Michael Ridgway