How to solve class xxx is not a valid entity or mapped superclass error
I have defined a class like:
<?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 the 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?
+3
source to share
2 answers
To solve this problem I had to call Setup :: createAnnotationMetadataConfiguration with false in the last parameter to avoid using 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);
+3
source to share