学说没有工作,除非我明确地调用AnnotationDriver :: getAllClassNames [英] Doctrine not working unless I explicitly call AnnotationDriver::getAllClassNames
问题描述
我试图使用Doctrine ORM的第一次,而我继<一个href=\"http://www.doctrine-project.org/docs/orm/2.0/en/reference/configuration.html#obtaining-an-entitymanager\">configuration这里找到。
按照以下步骤直接导致了以下错误:
警告: class_parents()[function.class父母]:类
MyProject的\\型号\\用户不存在
并且不能在被加载
/opt/local/lib/php/Doctrine/ORM/Mapping/ClassMetadataFactory.php
在网上的 222
警告: array_reverse()预计参数1是阵列,鉴于布尔
在
/opt/local/lib/php/Doctrine/ORM/Mapping/ClassMetadataFactory.php
在网上的 222
警告:提供的foreach无效参数()的
/opt/local/lib/php/Doctrine/ORM/Mapping/ClassMetadataFactory.php
在网上的 222
致命错误:未捕获的异常'ReflectionException与消息
类MyProject的\\型号\\用户不
存在'在
/opt/local/lib/php/Doctrine/ORM/Mapping/ClassMetadata.php:67
堆栈跟踪:
#0 /opt/local/lib/php/Doctrine/ORM/Mapping/ClassMetadata.php(67):
ReflectionClass - > __构造('MyProject的\\型号...')
#1 /opt/local/lib/php/Doctrine/ORM/Mapping/ClassMetadataFactory.php(350):
学说\\ ORM \\制图\\ ClassMetadata - > __构造('MyProject的\\型号...')
2 /opt/local/lib/php/Doctrine/ORM/Mapping/ClassMetadataFactory.php(260):
学说\\ ORM \\制图\\ ClassMetadataFactory-> newClassMetadataInstance('MyProject的\\型号...')
#3 /opt/local/lib/php/Doctrine/ORM/Mapping/ClassMetadataFactory.php(169):
学说\\ ORM \\制图\\ ClassMetadataFactory-> loadMetadata('MyProject的\\型号...')
#4 /opt/local/lib/php/Doctrine/ORM/EntityManager.php(247):
学说\\ ORM \\制图\\ ClassMetadataFactory-> getMetadataFor('MyProject的\\型号...')
#5 /opt/local/lib/php/Doctrine/ORM/EntityManager.php(563):
学说\\ ORM \\ EntityManager-> getClassMetadata('MyProject的\\型号...')
#6 /选择适用的 /opt/local/lib/php/Doctrine/ORM/Mapping/ClassMetadata.php
在线 67
块引用>此错误消失,而code完美地工作,如果在样品code,我添加以下行(与下面的评论表示):
$ driverImpl =组$ config-&GT; newDefaultAnnotationDriver('/路径/要/ lib中/ MyProject的/实体');
$ driverImpl-&GT; getAllClassNames(); // **我添加的LINE **
组$ config-&GT; setMetadataDriverImpl($ driverImpl);有什么,提到这个调用被要求得到样品code工作;这只是我的东西发生了跨越呼应的同时一些信息到屏幕上,同时试图看到我的错误发生跌倒。
有没有需要这个调用(以及为什么它没有在样品code提到的)理由吗?有没有别的东西,应该被称作,这是根本原因,以我原来的错误?
(我可以发布更多code的要求,我只是不积极是什么soliving这个,因为它是使用Doctrine我第一次最有帮助的。)
编辑:我忘了提及在实际发生错误的行:
$ USER = $的EM&GT;找到('MyProject的\\型号\\用户,1);
解决方案这听起来像你没有一个类加载器设置为项目正常:
$类加载器=新\\主义\\ COMMON \\的ClassLoader('MyProject的','/路径/要/ lib中/');
$ classloader-&GT;注册();原因调用getAllClassNames()被修复您的问题,是因为它遍历所有的目录和文件的/路径下的/到/ lib中/ MyProject的/实体,包括他们。显然,这是缓慢的,因此不推荐。
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);
解决方案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.
这篇关于学说没有工作,除非我明确地调用AnnotationDriver :: getAllClassNames的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!