murze murze - 6 months ago 36
PHP Question

How to solve Class xxx is not a valid entity or mapped super class error

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?

Answer

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