3

I have defined a class Setting like this:

<?php

use Doctrine\ORM\Mapping as ORM;

/** @ORM\Entity */
class Setting {
    /** @ORM\Column(type="integer")
     *  @ORM\GeneratedValue
     *  @ORM\id
     */
    private $id;

    /** @ORM\Column(length=255, nullable=true) */
    private $displayname;

    /** @ORM\Column(length=255) */
    private $name;

    /** @ORM\Column(type="text") */
    private $value;

    public function __get($name) {
        return $this->$name;
    }

    public function __set($key, $value){
        $this->$key = $value;
    }

    public function getFullName() {
        return $this->name . ' suffix';
    }


    public static function getValue($settingName) {
        $result = '';
        try {
            $setting = em()->createQuery('SELECT s FROM Setting s WHERE s.name = :name')
                ->setParameter('name', $settingName)
                ->getSingleResult();

            $result = $setting->value;
        }
        catch (\Doctrine\ORM\NoResultException $exception) {

        }

        return $result;
    }
}

Unfortunately this gives an error Fatal error: Uncaught exception 'Doctrine\ORM\Mapping\MappingException' with message 'Class "Setting" is not a valid entity or mapped super class.' in xxxxx/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/MappingException.php on line 216

How can this be solved?

murze
  • 4,015
  • 8
  • 43
  • 70
  • in what namespace and folder is the entity? check http://stackoverflow.com/questions/7820597/class-xxx-is-not-a-valid-entity-or-mapped-super-class-after-moving-the-class-i – user1236048 Feb 13 '13 at 10:27

2 Answers2

3

To solve this problem I had to call Setup::createAnnotationMetadataConfiguration with false in the last parameter to avoid the use of SimpleAnnotationReader because you need AnnotationReader to use the ORM\ namespace prefix.

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

$dbParams = array(
    'driver' => 'pdo_mysql',
    'host' => '127.0.0.1',
    'user' => 'root',
    'password' => '', 
    'dbname' => 'db',
);

$path = __DIR__ . '/Entity';

$config = Setup::createAnnotationMetadataConfiguration(array($path), true, null, null, false);

$entityManager = EntityManager::create($dbParams, $config);
Pete
  • 1,305
  • 1
  • 12
  • 36
Gabriel
  • 1,890
  • 1
  • 17
  • 23
1

Found the solution: it seems that the problem lies with the use of SimpleAnnotationReader.

When configurating the annotationDriver like this (second parameter should be false) it works.

$driver = $config->newDefaultAnnotationDriver(
    APPLICATION_PATH . '/models', false
);
murze
  • 4,015
  • 8
  • 43
  • 70