Not a valid entity or mapped super class error from Doctrine

Go To StackoverFlow.com

5

I am trying to set up Doctrine (2.2.1) to use with my site and I followed the getting started guide and I am getting the following error:

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 is defined as follows (in 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;
    }
}

?>

Doctrine is included like this in the page:

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

The doctrine-configure.php file:

//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);

?>

And finally the code that causes the crash:

require "$root/Doctrine/entities/DocumentField.php";
$field = new DocumentField();
$field->setName("Hello World");
$field->setType(1);
$field->setRequired(1);
$entityManager->persist($field);
$entityManager->flush();
2012-04-03 23:33
by Sam
If configure correctly, doctrine should autoload the entity classes. Requiring them like that is not the right way. Re-read the configuration part of the manual : - Flukey 2012-04-03 23:41
try this: $config->newDefaultAnnotationDriver($GLOBALS["BASE_PATH"].'/Doctrine/entities');

P.S. you can use Doctrine ClassLoader. It can resolve $class = new \Namespace\Entity\Class; to docRoot/Namespace/Entity/Class.php and require it automatic - CappY 2012-09-21 21:18

Looks like a mapping problem, so can you include your mapping file as well - eddy147 2012-10-06 05:23


3

Each Doctrine annotation is actually a representation of an instance of a class, so you have to specify a namespace (\Doctrine\ORM\Mapping) for each annotation if you are not inside such namespace, just as if you are instantiating one of those classes by yourself.

Try it this way:

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

    // ...
2012-10-23 13:58
by Carlos
Could you please explain why this could be the solution? Thanks - kaiser 2013-09-17 01:28
Sure, it's simple. Each Doctrine annotation is actually a representation of an instance of a class, so you have to specify a namespace (\Doctrine\ORM\Mapping) for each annotation if you are not inside such namespace, just as if you are instantiating one of those classes by yourself - Carlos 2013-09-17 07:49
Thanks a lot. Now it made *click*. Should be an edit and +1 : - kaiser 2013-09-17 11:23
@kaiser Edited. Thank you - Carlos 2013-09-17 13:15


0

<?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);
?>
2014-09-05 14:18
by Fábio Vanderlei