首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不是来自Doctrine的有效实体或映射超类错误

问不是来自Doctrine的有效实体或映射超类错误
EN

Stack Overflow用户
提问于 2012-04-04 07:33:27
回答 2查看 4.8K关注 0票数 5

我正在尝试将Doctrine (2.2.1)设置为与我的站点一起使用,我遵循了入门指南,并收到以下错误:

代码语言:javascript
复制
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) {...}

DocumentField定义如下(在根目录/Doctrine/entities/DocumentField.php中:

代码语言:javascript
复制
<?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;
    }
}

?>

原理在页面中如下所示:

代码语言:javascript
复制
/* Load Doctorine ORM */
require "$root/Doctrine/ORM/Tools/Setup.php";
$lib = "$root/";
Doctrine\ORM\Tools\Setup::registerAutoloadDirectory($lib);
require "doctrine-configure.php";

doctrine-configure.php文件:

代码语言:javascript
复制
//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);

?>

最后是导致崩溃的代码:

代码语言:javascript
复制
require "$root/Doctrine/entities/DocumentField.php";
$field = new DocumentField();
$field->setName("Hello World");
$field->setType(1);
$field->setRequired(1);
$entityManager->persist($field);
$entityManager->flush();
EN

回答 2

Stack Overflow用户

发布于 2012-10-23 21:58:43

每个Doctrine注解实际上是一个类实例的表示,所以如果您不在这样的命名空间中,则必须为每个注解指定一个命名空间(\Doctrine\ORM\Mapping),就像您自己实例化其中一个类一样。

这样试试:

代码语言:javascript
复制
<?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;

    // ...
票数 3
EN

Stack Overflow用户

发布于 2014-09-05 22:18:05

代码语言:javascript
复制
<?php
// bootstrap or config.php
require __DIR__ . '/../vendor/autoload.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;


$paths = array('PATH_WITH_ENTITIES');
$isDevMode = false;

// the connection configuration
$dbParams = array(
    'driver'   => 'pdo_mysql',
    'user'     => 'USER',
    'password' => 'PASS',
    'host'     => 'HOST',
    'dbname'   => 'DB',
);

$classLoader = new Doctrine\Common\ClassLoader('Application\Entity',__DIR__.'/../admin/module/Application/src/');
$classLoader->register();

$config = Setup::createAnnotationMetadataConfiguration($paths, $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($dbParams, $config);
?>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10002972

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档