“类 XXX 不是有效的实体或映射的超类”在文件系统中移动类之后
我在 Aib\PlatformBundle\Entity\User.php 中有一个实体类,
尝试通过以下方式创建其表单类没有问题
php 应用程序/控制台学说:生成:表单 AibPlatformBundle:用户
现在我已将命名空间更改为 Aib\PlatformBundle\Entity\Identity\User,但是当我尝试使用我之前所说的任务生成表单时 它说:
“Aib\PlatformBundle\Entity\User 类不是有效实体或映射 超级一流。”
这是文件内容:
<?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
{
...
知道吗?
symfony2.0.4
I had an entity class in Aib\PlatformBundle\Entity\User.php
I had no problems trying to create its form class through
php app/ console doctrine:generate:form AibPlatformBundle:User
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:
"Class Aib\PlatformBundle\Entity\User is not a valid entity or mapped
super class."
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
遇到这个问题 - 不要忘记注释
* @ORM\Entity
如下所示:Had this problem - don't forget the annotation
* @ORM\Entity
like below:昨天遇到这个问题,发现了这个线程。我在新包(例如 MyFooBundle/Entity/User.php)中创建了具有映射的实体,根据文档完成了所有配置,但在尝试加载应用程序时遇到了与上面相同的错误。
最后我意识到我没有在 AppKernel 中加载 MyFooBundle:
调试此问题的一个好方法是运行以下命令:
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.
In the end I realized that I wasn't loading MyFooBundle in AppKernel:
A great way to debug this is to run this command:
请检查您的 config.yml 文件,应包含类似以下内容:
将您自己的包添加到映射列表中。
Do check your config.yml file, should be containing something like this:
Add your own bundle to the mappings list.
我通过在创建
MetaDataConfiguration
时设置$useSimpleAnnotationReader=false
解决了此问题。I resolved this issue by setting
$useSimpleAnnotationReader=false
when creating theMetaDataConfiguration
.我通过将
false
作为第二个参数传递给Doctrine\ORM\Configuration::newDefaultAnnotationDriver
解决了这个问题。我花了一段时间去谷歌和源代码进行挖掘。
我的情况有点特殊,因为我使用的映射指向与 Symfony 安装无关的另一个目录,因为我还必须使用遗留代码。
我重构了遗留实体,但它们停止了工作。他们过去使用@Annotation而不是@ORM\Annotation,因此重构后根本无法读取元数据。通过不使用简单的注释阅读器,一切似乎都还不错。
I solved this by passing
false
as the second parameter toDoctrine\ORM\Configuration::newDefaultAnnotationDriver
.It took me a while of digging through Google and source code.
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.
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.就我而言,通过将服务器缓存从eAccelerator更改为APC解决了问题。
显然,eAccelerator 会从文件中删除所有会破坏注释的注释。
In my case the problem was solved by changing my servers cache from eAccelerator to APC.
Apparently eAccelerator strips all the comments from files which breaks your annotations.
非常感谢 Mark Fu 和 mogoman,
我知道它必须位于 config.yml 中的某个位置......并且能够针对它进行测试
确实有帮助!
事实上,这个命令只是在出现错误时停止...没有反馈,但是当一切正常时,您应该能够看到列出的所有实体。
big thx to Mark Fu and mogoman
I knew it had to be somewhere in the config.yml... and being able to test it against the
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.
我通过删除捆绑包的 Resources/config/doctrine 文件夹中冲突的自动生成的 orm.php 文件解决了相同的异常;根据文档:“一个包只能接受一种元数据定义格式。例如,不可能将 YAML 元数据定义与带注释的 PHP 实体类定义混合在一起。”
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."
就我而言,我正在迁移到 PHP 注释,但仍然有此
删除它解决了问题。
In my case, i was migrating to the PHP annotations but still had this
Removing it solved the issue.
您很有可能拥有 PHP 5.3.16(Symfony 2.x 无法使用它)。无论如何,您应该在 http://you.site.name/config.php 上加载检查页面
如果您的项目无法在托管服务器上运行,则必须删除“config.php”中的下一行:
祝你好运!
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.php
If you had project not worked on hosting server, next lines must be removed in "config.php":
Goodluck!
我的错误是我正在做
而不是
My mistake was I was doing
instead of
就我而言,我在重构过程中过于热心,删除了一个 yml 文件!
In my case, I was too zealous during a refactor and had deleted a doctrine yml file!
就我在 Mac 上的情况而言,我使用的是 src/MainBundle/Resource/Config/Doctrine,当然它可以在 Mac 上运行,但不能在生产 Ubuntu 服务器上运行。将 Config 重命名为 config,将 Doctrine 重命名为 Doctrine 后,就找到了映射文件并开始工作。
In my case on my mac I was using src/MainBundle/Resource/Config/Doctrine, of course it worked on Mac but it didn't work on production Ubuntu server. Once renamed Config to config and Doctrine to doctrine, the mapping files were found and it started working.
在我的情况下,在制作 make:entity 后,我尝试了以下命令
php bin/consoledoctrine:mapping:import "App\Entity"annotation --path=src/Entity
从数据库生成实体
但是,此命令不提供 getter 和 setter,如果您在控制器内使用它,则会导致未知方法错误(例如 getId),否则您稍后将使用它所以我决定返回到从
so 生成的实体我可以恢复丢失的方法,但不幸的是这导致我错误类不是有效的实体或映射的超类。
接下来为了防止此错误,我没有耐心阅读此文档
[1]: https://www.doctrine-project.org/projects/doctrine-orm/en/2.11/tutorials/override-field-association-mappings-in-subclasses.html#override-field-association-mappings-in -子类 特别是我使用属性代替注释,
因此我带回了生成的实体,只需添加以下命令即可生成 getters 和 setters
$ php bin/console make:entity --regenerate App
救了我的命,虽然这解决了我的问题,但我不明白为什么在第一种情况下它导致我这个主题的错误
In my case after making make:entity i tried the following command
php bin/console doctrine:mapping:import "App\Entity" annotation --path=src/Entity
which generates the entity from the database
However, this Command don t provide the getters and setters that will cause you unknown method error (getId for example) if you are using it inside a controller or you ll use it later So i decided to go back to the generated entity from the
so i can bring back the missing methods but unfortunately this caused me the error class is not a valid entity or mapped superclass.
next to prevent this error i haven 't patience to read this documentation
[1]: https://www.doctrine-project.org/projects/doctrine-orm/en/2.11/tutorials/override-field-association-mappings-in-subclasses.html#override-field-association-mappings-in-subclasses especially i m using attributes in the place of annotation,
therefore i brought back the generated entity and just adding the following command which generates getters and setters
$ php bin/console make:entity --regenerate App
saved my life ,though this solved my problem but i didn t figure out why in the first case it caused me the error of this topic
我通过使用 app/console_dev 而不仅仅是 app/console 摆脱了与您的情况相同的错误消息
I got rid of the same error message as in your case by using app/console_dev instead of just app/console