Doctrine2 - “class” is not a valid entity or mapped super class

后端 未结 5 2061
庸人自扰
庸人自扰 2020-12-05 06:42

I get exception Uncaught exception \'Doctrine\\ORM\\Mapping\\MappingException\' with message \'Class \"Users\" is not a valid entity or mapped super class every

5条回答
  •  时光取名叫无心
    2020-12-05 07:19

    You are using a Doctrine\Common\Annotations\SimpleAnnotationReader instead of a Doctrine\Common\Annotations\AnnotationReader.

    The SimpleAnnotationReader works with default namespaces and reads annotations in format @Entity, while the AnnotationReader can use the imported classes and namespaces (via use statement) and annotations such as @ORM\Entity.

    You can read more about that on the documentation.

    Here's a fixed version of your test.php

     'pdo_mysql',
        'user'     => 'root',
        'password' => 'pass',
        'dbname'   => 'dbname',
    );
    
    $config = Setup::createConfiguration($isDevMode);
    $driver = new AnnotationDriver(new AnnotationReader(), $paths);
    
    // registering noop annotation autoloader - allow all annotations by default
    AnnotationRegistry::registerLoader('class_exists');
    $config->setMetadataDriverImpl($driver);
    
    $em = EntityManager::create($connectionParams, $config);
    
    $user = $em->find('Users', 5);
    

提交回复
热议问题