Doctrine does not work unless I explicitly call AnnotationDriver :: getAllClassNames

I am trying to use Doctrine ORM for the first time, and I am following the configuration found here .

As a result of these actions, the following error occurs:

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

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

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

Fatal error: Unused exception "ReflectionException" with the 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): Learning \ ORM \ Mapping \ ClassMetadata β†’ __ construct ('MyProject \ Model ...')

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

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

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

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

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

This error disappears and the code works fine if in the code example I add the following line (indicated by the comment below):

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

There is nothing to indicate that this call is required to make the sample code work; it was just that I accidentally stumbled upon repeating some information on the screen, trying to see where my error was happening.

Is there a reason this call is necessary (and why it is not mentioned in the sample code)? Is there anything else that should be called instead, which is the main reason for my initial mistake?

(I can put more code as needed, I'm just not sure what is most useful in solving this since I first used Doctrine.)

EDIT: I forgot to mention the line on which the error occurs:

 $user = $em->find('MyProject\Model\User', 1); 
+6
php orm annotations doctrine2
source share
2 answers

It looks like you do not have the correct classloader for your project:

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

The reason calling getAllClassNames () fixes your problem because it goes through all the directories and files inside / path / to / lib / MyProject / Entities and includes them. Obviously, this is slow and not recommended.

+7
source share

In addition, you cannot load objects without a namespace. These classes do not load automatically.

0
source share

All Articles