【问题标题】:Doctrine2 - "class" is not a valid entity or mapped super classDoctrine2 - “类”不是有效的实体或映射的超类
【发布时间】:2013-02-12 11:46:54
【问题描述】:

每次运行下一个代码时,我都会收到异常Uncaught exception 'Doctrine\ORM\Mapping\MappingException' with message 'Class "Users" is not a valid entity or mapped super class

test.php

<?php
require_once "vendor/autoload.php";

use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;

$paths = array(dirname(__FILE__)."/entities");
$isDevMode = false;

// the connection configuration
$dbParams = array(
    'driver'   => 'pdo_mysql',
    'user'     => 'root',
    'password' => 'pass',
    'dbname'   => 'snabcentr',
);

$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
$em = EntityManager::create($dbParams, $config);

$user = $em->find("Users", 5);

entities/Users.php

<?php
use Doctrine\ORM\Mapping as ORM;

/**
 * Users
 *
 * @ORM\Table(name="users")
 * @ORM\Entity
 */
class Users
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="email", type="string", length=255, nullable=true)
     */
    private $email;

    /**
     * @var string
     *
     * @ORM\Column(name="password", type="string", length=255, nullable=true)
     */
    private $password;

    /**
     * @var string
     *
     * @ORM\Column(name="type", type="string", nullable=true)
     */
    private $type;

    /**
     * @var string
     *
     * @ORM\Column(name="client_inn", type="string", length=255, nullable=true)
     */
    private $clientInn;

    /**
     * @var string
     *
     * @ORM\Column(name="client_ogrn", type="string", length=255, nullable=true)
     */
    private $clientOgrn;

    /**
     * @var string
     *
     * @ORM\Column(name="client_rs", type="string", length=255, nullable=true)
     */
    private $clientRs;

    /**
     * @var string
     *
     * @ORM\Column(name="client_ks", type="string", length=255, nullable=true)
     */
    private $clientKs;

    /**
     * @var string
     *
     * @ORM\Column(name="client_bik", type="string", length=255, nullable=true)
     */
    private $clientBik;

    /**
     * @var string
     *
     * @ORM\Column(name="client_uaddress", type="string", length=255, nullable=true)
     */
    private $clientUaddress;

    /**
     * @var string
     *
     * @ORM\Column(name="client_faddress", type="string", length=255, nullable=true)
     */
    private $clientFaddress;

    /**
     * @var string
     *
     * @ORM\Column(name="client_daddress", type="string", length=255, nullable=true)
     */
    private $clientDaddress;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255, nullable=true)
     */
    private $name;

    /**
     * @var string
     *
     * @ORM\Column(name="notes", type="text", nullable=true)
     */
    private $notes;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="added_date", type="datetime", nullable=true)
     */
    private $addedDate;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set email
     *
     * @param string $email
     * @return Users
     */
    public function setEmail($email)
    {
        $this->email = $email;

        return $this;
    }

    /**
     * Get email
     *
     * @return string 
     */
    public function getEmail()
    {
        return $this->email;
    }

    /**
     * Set password
     *
     * @param string $password
     * @return SnabUsers
     */
    public function setPassword($password)
    {
        $this->password = $password;

        return $this;
    }

    /**
     * Get password
     *
     * @return string 
     */
    public function getPassword()
    {
        return $this->password;
    }

    /**
     * Set type
     *
     * @param string $type
     * @return SnabUsers
     */
    public function setType($type)
    {
        $this->type = $type;

        return $this;
    }

    /**
     * Get type
     *
     * @return string 
     */
    public function getType()
    {
        return $this->type;
    }

    /**
     * Set clientInn
     *
     * @param string $clientInn
     * @return SnabUsers
     */
    public function setClientInn($clientInn)
    {
        $this->clientInn = $clientInn;

        return $this;
    }

    /**
     * Get clientInn
     *
     * @return string 
     */
    public function getClientInn()
    {
        return $this->clientInn;
    }

    /**
     * Set clientOgrn
     *
     * @param string $clientOgrn
     * @return SnabUsers
     */
    public function setClientOgrn($clientOgrn)
    {
        $this->clientOgrn = $clientOgrn;

        return $this;
    }

    /**
     * Get clientOgrn
     *
     * @return string 
     */
    public function getClientOgrn()
    {
        return $this->clientOgrn;
    }

    /**
     * Set clientRs
     *
     * @param string $clientRs
     * @return SnabUsers
     */
    public function setClientRs($clientRs)
    {
        $this->clientRs = $clientRs;

        return $this;
    }

    /**
     * Get clientRs
     *
     * @return string 
     */
    public function getClientRs()
    {
        return $this->clientRs;
    }

    /**
     * Set clientKs
     *
     * @param string $clientKs
     * @return SnabUsers
     */
    public function setClientKs($clientKs)
    {
        $this->clientKs = $clientKs;

        return $this;
    }

    /**
     * Get clientKs
     *
     * @return string 
     */
    public function getClientKs()
    {
        return $this->clientKs;
    }

    /**
     * Set clientBik
     *
     * @param string $clientBik
     * @return SnabUsers
     */
    public function setClientBik($clientBik)
    {
        $this->clientBik = $clientBik;

        return $this;
    }

    /**
     * Get clientBik
     *
     * @return string 
     */
    public function getClientBik()
    {
        return $this->clientBik;
    }

    /**
     * Set clientUaddress
     *
     * @param string $clientUaddress
     * @return SnabUsers
     */
    public function setClientUaddress($clientUaddress)
    {
        $this->clientUaddress = $clientUaddress;

        return $this;
    }

    /**
     * Get clientUaddress
     *
     * @return string 
     */
    public function getClientUaddress()
    {
        return $this->clientUaddress;
    }

    /**
     * Set clientFaddress
     *
     * @param string $clientFaddress
     * @return SnabUsers
     */
    public function setClientFaddress($clientFaddress)
    {
        $this->clientFaddress = $clientFaddress;

        return $this;
    }

    /**
     * Get clientFaddress
     *
     * @return string 
     */
    public function getClientFaddress()
    {
        return $this->clientFaddress;
    }

    /**
     * Set clientDaddress
     *
     * @param string $clientDaddress
     * @return SnabUsers
     */
    public function setClientDaddress($clientDaddress)
    {
        $this->clientDaddress = $clientDaddress;

        return $this;
    }

    /**
     * Get clientDaddress
     *
     * @return string 
     */
    public function getClientDaddress()
    {
        return $this->clientDaddress;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return SnabUsers
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set notes
     *
     * @param string $notes
     * @return SnabUsers
     */
    public function setNotes($notes)
    {
        $this->notes = $notes;

        return $this;
    }

    /**
     * Get notes
     *
     * @return string 
     */
    public function getNotes()
    {
        return $this->notes;
    }

    /**
     * Set addedDate
     *
     * @param \DateTime $addedDate
     * @return SnabUsers
     */
    public function setAddedDate($addedDate)
    {
        $this->addedDate = $addedDate;

        return $this;
    }

    /**
     * Get addedDate
     *
     * @return \DateTime 
     */
    public function getAddedDate()
    {
        return $this->addedDate;
    }
}

你有什么想法吗? eAccelerator 未设置。 Doctrine v 2.2、PHP v 5.3.22、zend 引擎 2.3.0

【问题讨论】:

  • 我不认为它需要按顺序排列,但我总是把 @ORM\Entity 符号放在第一位,你试过吗?您是否有其他加载正常的工作实体?也不相关......这个类代表一个单一的“用户”实体,所以使用单数作为类名(即用户)的好习惯。
  • 这没有帮助:(。我有更多实体,但我对所有实体都有同样的问题......这可能是一个简单的问题,但我找不到究竟是什么错了
  • 我没有在 Symfony2 之外使用过很多 D2...Setup::createAnnotationMetadataConfiguration 是否注册了注解?
  • 它使用注释元数据驱动程序创建配置。
  • 你能提供一个 gist 中的$config 对象的转储吗?

标签: php doctrine-orm


【解决方案1】:

您使用的是Doctrine\Common\Annotations\SimpleAnnotationReader 而不是Doctrine\Common\Annotations\AnnotationReader

SimpleAnnotationReader 使用默认命名空间并以@Entity 格式读取注解,而AnnotationReader 可以使用导入的类和命名空间(通过use statement)以及@ORM\Entity 等注解。

您可以在documentation 上阅读更多相关信息。

这是您的 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);

【讨论】:

  • Doctrine 2.3 的 Setup::createAnnotationMetadataConfiguration 有一个名为 $useSimpleAnnotationReader 的第五个参数,您可以传递 false 使其工作!
  • 救命稻草。作为刚接触 Doctrine 的人,这非常有帮助。
  • 如果您将教义用作独立组件而不是在 symfony 中,请使用 @Entity 而不是 @ORM\Entity
【解决方案2】:

您可以使用您的配置,只需提供 FALSE 即可使用简单的注释阅读器,就像那样。

<?php

//same code

$isSimpleMode = FALSE;
$proxyDir = null;
$cache = null;
$config = Setup::createAnnotationMetadataConfiguration(
$paths, $isDevMode, $proxyDir, $cache, $isSimpleMode
);

//same
?>

【讨论】:

    【解决方案3】:

    就我而言,我只是忘记添加@ORM\Entity,像这样:

    /**
     * Project\BackendBundle\Entity\Pedido
     *
     * @ORM\Table
     * @ORM\Entity
     * @ORM\HasLifecycleCallbacks
     */
    class Pedido
    

    【讨论】:

      【解决方案4】:

      就我而言,我收到此错误是因为我使用/*** 而不是/** 打开了文档块:

      /***
       * @entity
       * @table(name="bans")
       */
      

      【讨论】:

      • 我有/* 而不是/**。谢谢!
      【解决方案5】:

      按照奥克拉米乌斯的回答(这也救了我)

      如果您的实体上有一些自定义命名空间,请确保注释出现在命名空间声明之后,这是我工作的实体:

      <?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;
      

      然后在我的控制器中:

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

      成功了!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-02-05
        • 2016-09-09
        • 1970-01-01
        • 1970-01-01
        • 2018-06-06
        • 2018-01-15
        • 2014-01-20
        相关资源
        最近更新 更多