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

后端 未结 5 2047
庸人自扰
庸人自扰 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:11

    You can use your configuration, just provide FALSE to use simple annotation reader, like that.

    <?php
    
    //same code
    
    $isSimpleMode = FALSE;
    $proxyDir = null;
    $cache = null;
    $config = Setup::createAnnotationMetadataConfiguration(
    $paths, $isDevMode, $proxyDir, $cache, $isSimpleMode
    );
    
    //same
    ?>
    
    0 讨论(0)
  • 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

    <?php
    
    use Doctrine\ORM\Tools\Setup;
    use Doctrine\ORM\EntityManager;
    use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
    use Doctrine\Common\Annotations\AnnotationReader;
    use Doctrine\Common\Annotations\AnnotationRegistry;
    
    require_once __DIR__ . '/vendor/autoload.php';
    require_once __DIR__ . '/entities/Users.php';
    
    $paths            = array(__DIR__ . '/entities');
    $isDevMode        = false;
    $connectionParams = array(
        'driver'   => '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);
    
    0 讨论(0)
  • 2020-12-05 07:20

    In my case, I just forgot to add @ORM\Entity, like this:

    /**
     * Project\BackendBundle\Entity\Pedido
     *
     * @ORM\Table
     * @ORM\Entity
     * @ORM\HasLifecycleCallbacks
     */
    class Pedido
    
    0 讨论(0)
  • 2020-12-05 07:22

    In my case, I received this error because I opened the doc block with /*** instead of /**:

    /***
     * @entity
     * @table(name="bans")
     */
    
    0 讨论(0)
  • 2020-12-05 07:24

    Following to Ocramius's answer (which saved me as well)

    If you've got some custom namespacing on your Entities make sure that the annotations come after the namespace decleration, here is my Entity that worked:

    <?php
    
    
    namespace App\Models;
    
    /**
      * Books
      *
      * @ORM\Table(name="books")
      * @ORM\Entity
      */
    
      use Doctrine\ORM\Mapping as ORM;
    
    
      class Books
      {
      /**
       * @var integer
       *
       * @ORM\Column(name="id", type="integer", nullable=false)
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="IDENTITY")
       */
      private $id;
    
      /**
       * @var string
       *
       * @ORM\Column(name="name", type="string", length=20, nullable=true)
       */
      private $name;
    
      /**
       * @var string
       *
       * @ORM\Column(name="isbn", type="string", length=20, nullable=true)
       */
      private $isbn;
    

    Then in my controller:

       $entityManager->find('App\Models\Books', 1)
    

    Success!

    0 讨论(0)
提交回复
热议问题