How to fix class is not a valid entity or mapped super class?

☆樱花仙子☆ 提交于 2021-01-27 05:50:48

问题


So I'm trying to follow symfony2's tutorial on doctrine for my own website and modeling my User entity after their Product one.

Also, before anyone marks this a duplicate, I have already tried the solutions given in numerous other questions with no luck:

  • Not a valid entity or mapped super class
  • Doctrine class is not a valid entity or mapped super class
  • Symfony/Doctrine: Class is not a valid entity or mapped super class
  • symfony2 is not a valid entity or mapped super class
  • Symfony/Doctrine: Class is not a valid entity or mapped super class
  • "Class XXX is not a valid entity or mapped super class" after moving the class in the filesystem

and the list goes on

I have my entity class:

<?php
    namespace MySite\MyBundle\Entity;

    use Doctrine\ORM\Mapping as ORM;

    /**
     * @ORM\Entity
     * @ORM\Table(name="user")
     */
    class User
    {
        /**
         * @ORM\Column(type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        protected $id;

        /**
         * @ORM\Column(type="string", length=100)
         */
        protected $name;

        /**
         * @ORM\Column(type="string", length=64)
         */
        protected $password;
    }
?>

Now, I'm running the command:

$ php app/console doctrine:generate:entities MySite/MyBundle/Entity/User

to generate the accessor methods. However, when I do this, I get the error:

[Doctrine\ORM\Mapping\MappingException]
Class "MySite\MyBundle\Entity\User" is not a valid entity or mapped super class.

回答1:


Ok, I figured it out myself. My problem is that my config.yml was wrong. I was missing the auto_mapping: true line in my config.yml.

doctrine:
    # (dbal stuff here)

    orm:
        auto_generate_proxy_classes: "%kernel.debug%"
        auto_mapping: true

After adding that, everything auto-generates fine with the php app/console doctrine:generate:entities MySite/MyBundle/Entity/User line




回答2:


I had a similar issue and I found at the end the problem in my case was I missed the class that extends Bundle

namespace Acme\TagBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class AcmeTagBundle extends Bundle
{
}

and declaring that class in AppKernel.php under the bundles array.




回答3:


My problem was that I named the folder entity instead of Entity. When I fixed that it worked like a charm.



来源:https://stackoverflow.com/questions/20887211/how-to-fix-class-is-not-a-valid-entity-or-mapped-super-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!