“类 XXX 不是有效的实体或映射的超类”在文件系统中移动类之后

发布于 2024-12-11 07:23:04 字数 714 浏览 0 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

晒暮凉 2024-12-18 07:23:04

遇到这个问题 - 不要忘记注释 * @ORM\Entity 如下所示:

/**
 * Powma\ServiceBundle\Entity\User
 *
 * @ORM\Entity
 * @ORM\Table(name="users")
 */

Had this problem - don't forget the annotation * @ORM\Entity like below:

/**
 * Powma\ServiceBundle\Entity\User
 *
 * @ORM\Entity
 * @ORM\Table(name="users")
 */
愿得七秒忆 2024-12-18 07:23:04

昨天遇到这个问题,发现了这个线程。我在新包(例如 MyFooBundle/Entity/User.php)中创建了具有映射的实体,根据文档完成了所有配置,但在尝试加载应用程序时遇到了与上面相同的错误。

最后我意识到我没有在 AppKernel 中加载 MyFooBundle:

new My\FooBundle\MyFooBundle()

调试此问题的一个好方法是运行以下命令:

app/console doctrine:mapping:info

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:

new My\FooBundle\MyFooBundle()

A great way to debug this is to run this command:

app/console doctrine:mapping:info
忆离笙 2024-12-18 07:23:04

请检查您的 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: ~

将您自己的包添加到映射列表中。

Do check your config.yml file, should be containing something like this:

# 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.

娇纵 2024-12-18 07:23:04

我通过在创建 MetaDataConfiguration 时设置 $useSimpleAnnotationReader=false 解决了此问题。

I resolved this issue by setting $useSimpleAnnotationReader=false when creating the MetaDataConfiguration.

旧瑾黎汐 2024-12-18 07:23:04

我通过将 false 作为第二个参数传递给 Doctrine\ORM\Configuration::newDefaultAnnotationDriver 解决了这个问题。

我花了一段时间去谷歌和源代码进行挖掘。

我的情况有点特殊,因为我使用的映射指向与 Symfony 安装无关的另一个目录,因为我还必须使用遗留代码。

我重构了遗留实体,但它们停止了工作。他们过去使用@Annotation而不是@ORM\Annotation,因此重构后根本无法读取元数据。通过不使用简单的注释阅读器,一切似乎都还不错。

I solved this by passing false as the second parameter to Doctrine\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.

阳光下的泡沫是彩色的 2024-12-18 07:23:04

就我而言,通过将服务器缓存从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.

怎会甘心 2024-12-18 07:23:04

非常感谢 Mark Fu 和 mogoman,

我知道它必须位于 config.yml 中的某个位置......并且能够针对它进行测试

app/console doctrine:mapping:info

确实有帮助!

事实上,这个命令只是在出现错误时停止...没有反馈,但是当一切正常时,您应该能够看到列出的所有实体。

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

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.

饮湿 2024-12-18 07:23:04

我通过删除捆绑包的 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."

胡渣熟男 2024-12-18 07:23:04

就我而言,我正在迁移到 PHP 注释,但仍然有此

orm:
    auto_generate_proxy_classes: true

删除它解决了问题。

In my case, i was migrating to the PHP annotations but still had this

orm:
    auto_generate_proxy_classes: true

Removing it solved the issue.

调妓 2024-12-18 07:23:04

您很有可能拥有 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.');
}

祝你好运!

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":

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!

不美如何 2024-12-18 07:23:04
  1. 实体必须具有正确的实体和表注释(顺序可能很重要,因此两者都尝试)
  2. 如果有自定义存储库,则必须通过实体类本身访问它($entityManager->getRepository('your ENTITY class name')),通过调用它Repository 类名会欺骗 Doctrine 认为 Repo 类本身一定是一个实体。愚蠢,但教义并没有做出严格的区分。
  1. Entity must have proper Entity and Table annotations (order may matter so try both)
  2. If there is custom repository it MUST be accessed via Entity class itself ($entityManager->getRepository('your ENTITY class name')), as calling it via Repository class name will trick Doctrine into thinking Repo class must be an entity itself. Silly, but Doctrine does not make hard distinction.
ぇ气 2024-12-18 07:23:04

我的错误是我正在做

$em->getRepository(EntityRepository::class)

而不是

$em->getRepository(Entity::class)

My mistake was I was doing

$em->getRepository(EntityRepository::class)

instead of

$em->getRepository(Entity::class)
在你怀里撒娇 2024-12-18 07:23:04

就我而言,我在重构过程中过于热心,删除了一个 yml 文件!

In my case, I was too zealous during a refactor and had deleted a doctrine yml file!

ゞ记忆︶ㄣ 2024-12-18 07:23:04

就我在 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.

美人如玉 2024-12-18 07:23:04

在我的情况下,在制作 make:entity 后,我尝试了以下命令

php bin/consoledoctrine:mapping:import "App\Entity"annotation --path=src/Entity
从数据库生成实体
但是,此命令不提供 getter 和 setter,如果您在控制器内使用它,则会导致未知方法错误(例如 getId),否则您稍后将使用它所以我决定返回到从

php bin/console make:entity

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

php bin/console make:entity

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

左耳近心 2024-12-18 07:23:04

我通过使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文