php 在文件系统中移动类后“类 XXX 不是有效的实体或映射的超类”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7820597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Class XXX is not a valid entity or mapped super class" after moving the class in the filesystem
提问by ziiweb
I had an entity class in Aib\PlatformBundle\Entity\User.php
我在 Aib\PlatformBundle\Entity\User.php 中有一个实体类
I had no problems trying to create its form class through
我尝试通过创建它的表单类没有问题
php app/ console doctrine:generate:form AibPlatformBundle:User
php 应用程序/控制台学说:生成:表单 AibPlatformBundle:用户
Now I have change the namespace to Aib\PlatformBundle\Entity\Identity\User, but when I try to generate the form with the task I said before it says:
现在我已将命名空间更改为 Aib\PlatformBundle\Entity\Identity\User,但是当我尝试使用我之前说过的任务生成表单时,它说:
"Class Aib\PlatformBundle\Entity\User is not a valid entity or mapped super class."
“类 Aib\PlatformBundle\Entity\User 不是有效的实体或映射的超类。”
This is the file content:
这是文件内容:
<?php
namespace Aib\PlatformBundle\Entity\Identity;
use Doctrine\ORM\Mapping as ORM;
/**
* Aib\PlatformBundle\Entity\Identity\User
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="Aib\PlatformBundle\Entity\Identity
\UserRepository")
*/
class User
{
...
Any idea?
任何的想法?
symfony2.0.4
symfony2.0.4
回答by Mike
Had this problem - don't forget the annotation * @ORM\Entity
like below:
有这个问题 - 不要忘记如下注释* @ORM\Entity
:
/**
* Powma\ServiceBundle\Entity\User
*
* @ORM\Entity
* @ORM\Table(name="users")
*/
回答by mogoman
Had this problem yesterday and found this thread. I created the entity with the mapping in a new bundle (e.g. MyFooBundle/Entity/User.php), did all the configuration according to the docs but got the same error from above when trying to load the app.
昨天遇到了这个问题,找到了这个线程。我在一个新包(例如 MyFooBundle/Entity/User.php)中创建了带有映射的实体,根据文档进行了所有配置,但在尝试加载应用程序时从上面得到了相同的错误。
In the end I realized that I wasn't loading MyFooBundle in AppKernel:
最后我意识到我没有在 AppKernel 中加载 MyFooBundle:
new My\FooBundle\MyFooBundle()
A great way to debug this is to run this command:
一个很好的调试方法是运行这个命令:
app/console doctrine:mapping:info
回答by Mark Fu
Do check your config.yml file, should be containing something like this:
请检查您的 config.yml 文件,应该包含如下内容:
# Doctrine Configuration
doctrine:
dbal:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
charset: UTF8
types:
json: Sonata\Doctrine\Types\JsonType
orm:
auto_generate_proxy_classes: %kernel.debug%
# auto_mapping: true
entity_managers:
default:
mappings:
FOSUserBundle: ~
# ApplicationSonataUserBundle: ~
YourUserBundle: ~
SonataUserBundle: ~
Add your own bundle to the mappings list.
将您自己的包添加到映射列表中。
回答by wucdbm
I solved this by passing false
as the second parameter to Doctrine\ORM\Configuration::newDefaultAnnotationDriver
.
我通过将false
第二个参数传递给Doctrine\ORM\Configuration::newDefaultAnnotationDriver
.
It took me a while of digging through Google and source code.
我花了一些时间来挖掘 Google 和源代码。
My case was sort of special since I was using a mapping pointing to another directory unrelated to the Symfony installation as I also had to use legacy code.
我的情况有点特殊,因为我使用的映射指向另一个与 Symfony 安装无关的目录,因为我还必须使用遗留代码。
I had refactored legacy entities and they stopped working. They used to use @Annotation
instead of @ORM\Annotation
, so after refactoring it simply failed to read the metadata. By not using a simple annotation reader, everything seems to be okayish.
我重构了遗留实体,但它们停止工作。他们曾经使用@Annotation
而不是@ORM\Annotation
,因此在重构后它根本无法读取元数据。通过不使用简单的注释阅读器,一切似乎都很好。
回答by gruentee
I resolved this issue by setting $useSimpleAnnotationReader=false
when creating the MetaDataConfiguration
.
我通过$useSimpleAnnotationReader=false
在创建MetaDataConfiguration
.
回答by Christian Vermeulen
In my case the problem was solved by changing my servers cache from eAcceleratorto APC. Apparently eAccelerator strips all the comments from files which breaks your annotations.
在我的情况下,问题是通过将我的服务器缓存从eAccelerator更改为APC 来解决的。显然,eAccelerator 会从破坏注释的文件中删除所有注释。
回答by MediaVince
big thx to Mark Fu and mogoman
非常感谢 Mark Fu 和 mogoman
I knew it had to be somewhere in the config.yml... and being able to test it against the
我知道它必须在 config.yml 中的某个地方...并且能够针对
app/console doctrine:mapping:info
really helped!
真的有帮助!
In fact, this command just simply stops at an error... no feedback, but when everything is fine you should be able to see all your entities listed.
事实上,这个命令只是在出现错误时停止……没有反馈,但是当一切正常时,您应该能够看到列出的所有实体。
回答by Cartesian Theater
I resolved the same exception by deleting a conflicting autogenerated orm.php file in the bundle's Resources/config/doctrine folder; according to the documentation: "A bundle can accept only one metadata definition format. For example, it's not possible to mix YAML metadata definitions with annotated PHP entity class definitions."
我通过删除包的 Resources/config/doctrine 文件夹中的一个冲突的自动生成的 orm.php 文件解决了同样的异常;根据文档:“一个包只能接受一种元数据定义格式。例如,不可能将 YAML 元数据定义与带注释的 PHP 实体类定义混合使用。”
回答by Stanislav Terletskyi
Very high possibility that you have PHP 5.3.16 (Symfony 2.x will not work with it). Anyway you should load check page on http://you.site.name/config.phpIf you had project not worked on hosting server, next lines must be removed in "config.php":
您使用 PHP 5.3.16 的可能性非常高(Symfony 2.x 无法使用它)。无论如何,您应该在http://you.site.name/config.php上加载检查页面 如果您的项目不在托管服务器上运行,则必须在“config.php”中删除下一行:
if (!in_array(@$_SERVER['REMOTE_ADDR'], array(
'127.0.0.1',
'::1',
))) {
header('HTTP/1.0 403 Forbidden');
exit('This script is only accessible from localhost.');
}
Goodluck!
祝你好运!
回答by jhchnc
In my case, I was too zealous during a refactor and had deleted a doctrine yml file!
就我而言,我在重构过程中太热心了,删除了一个学说 yml 文件!