不是Doctrine中的有效实体或映射的超类错误

Sam*_*Sam 5 php doctrine doctrine-orm

我正在尝试设置Doctrine(2.2.1)以使用我的网站,我按照入门指南进行操作,我收到以下错误:

Fatal error: Uncaught exception 'Doctrine\ORM\Mapping\MappingException' with message 'Class   DocumentField is not a valid entity or mapped super class.' in C:\inetpub\sites\hd\Doctrine\ORM\Mapping\MappingException.php:147 Stack trace: #0 C:\inetpub\sites\hd\Doctrine\ORM\Mapping\Driver\AnnotationDriver.php(165) {...}
Run Code Online (Sandbox Code Playgroud)

DocumentField 定义如下(在root/Doctrine/entities/DocumentField.php中:

<?php
/** @Entity **/
/** @Table(name="DocumentFields") */
class DocumentField
{
    /** @Id @GeneratedValue @Column(type="integer") **/
    protected $id;
    /** @var @Column(type="string") **/
    protected $fieldName;
    /** @var @Column(type="integer") **/
    protected $fieldType;
    /** @var @Column(type="integer") **/
    protected $required;

    public function getId() {
        return $this->id;
    }

    public function getName() {
        return $this->fieldName;
    }

    public function setName($name) {
        $this->fieldName = $name;
    }

    public function getType() {
        return $this->fieldType;
    }

    public function setType($type) {
        $this->fieldType = $type;
    }

    public function getRequired() {
        return $this->required;
    }

    public function setRequired($value) {
        $this->required = $value;
    }
}
Run Code Online (Sandbox Code Playgroud)

?>

Doctrine在页面中包含如下:

/* Load Doctorine ORM */
require "$root/Doctrine/ORM/Tools/Setup.php";
$lib = "$root/";
Doctrine\ORM\Tools\Setup::registerAutoloadDirectory($lib);
require "doctrine-configure.php";
Run Code Online (Sandbox Code Playgroud)

doctrine-configure.php文件中:

//if ($applicationMode == "development") {
    $cache = new \Doctrine\Common\Cache\ArrayCache;
//} else {
//    $cache = new \Doctrine\Common\Cache\ApcCache;
//}

$config = new Configuration;
$config->setMetadataCacheImpl($cache);
$driverImpl = $config->newDefaultAnnotationDriver($GLOBALS["BASE_PATH"].'\\Doctrine\\entities');
$config->setMetadataDriverImpl($driverImpl);
$config->setQueryCacheImpl($cache);
$config->setProxyDir($GLOBALS["BASE_PATH"].'\\Doctrine\\proxies');
$config->setProxyNamespace('Helpdesk\Proxies');

//if ($applicationMode == "development") {
    $config->setAutoGenerateProxyClasses(true);
//} else {
//    $config->setAutoGenerateProxyClasses(false);
//}

$connectionOptions = array(
    'driver' => 'pdo_sqlsrv',
    'user' => '{user}',
    'password' => '{pass}',
    'host' => 'sql1',
    'dbname' => 'HD'
);

$entityManager = EntityManager::create($connectionOptions, $config);
Run Code Online (Sandbox Code Playgroud)

?>

最后导致崩溃的代码:

require "$root/Doctrine/entities/DocumentField.php";
$field = new DocumentField();
$field->setName("Hello World");
$field->setType(1);
$field->setRequired(1);
$entityManager->persist($field);
$entityManager->flush();
Run Code Online (Sandbox Code Playgroud)

Car*_*los 3

每个 Doctrine 注释实际上是一个类实例的表示,因此如果您不在这样的命名空间内,则必须为每个注释指定一个命名空间 (\Doctrine\ORM\Mapping),就像您正在实例化这些类之一一样靠你自己。

试试这样:

<?php

use \Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="DocumentFields")
 */
class DocumentField
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    protected $id;

    /**
     * @ORM\Column(type="string")
     */
    protected $fieldName;

    /**
     * @ORM\Column(type="integer")
     */
    protected $fieldType;

    /**
     * @ORM\Column(type="integer")
     */
    protected $required;

    // ...
Run Code Online (Sandbox Code Playgroud)

  • 当然,这很简单。每个 Doctrine 注释实际上是一个类实例的表示,因此如果您不在这样的名称空间内,则必须为每个注释指定一个名称空间 (`\Doctrine\ORM\Mapping`),就像您实例化其中之一一样这些课程都是你自己做的。 (2认同)