I get exception Uncaught exception \'Doctrine\\ORM\\Mapping\\MappingException\' with message \'Class \"Users\" is not a valid entity or mapped super class
every
You can use your configuration, just provide FALSE to use simple annotation reader, like that.
<?php
//same code
$isSimpleMode = FALSE;
$proxyDir = null;
$cache = null;
$config = Setup::createAnnotationMetadataConfiguration(
$paths, $isDevMode, $proxyDir, $cache, $isSimpleMode
);
//same
?>
You are using a Doctrine\Common\Annotations\SimpleAnnotationReader
instead of a Doctrine\Common\Annotations\AnnotationReader
.
The SimpleAnnotationReader
works with default namespaces and reads annotations in format @Entity
, while the AnnotationReader
can use the imported classes and namespaces (via use statement) and annotations such as @ORM\Entity
.
You can read more about that on the documentation.
Here's a fixed version of your test.php
<?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;
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/entities/Users.php';
$paths = array(__DIR__ . '/entities');
$isDevMode = false;
$connectionParams = array(
'driver' => 'pdo_mysql',
'user' => 'root',
'password' => 'pass',
'dbname' => 'dbname',
);
$config = Setup::createConfiguration($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($connectionParams, $config);
$user = $em->find('Users', 5);
In my case, I just forgot to add @ORM\Entity
, like this:
/**
* Project\BackendBundle\Entity\Pedido
*
* @ORM\Table
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
*/
class Pedido
In my case, I received this error because I opened the doc block with /***
instead of /**
:
/***
* @entity
* @table(name="bans")
*/
Following to Ocramius's answer (which saved me as well)
If you've got some custom namespacing on your Entities make sure that the annotations come after the namespace decleration, here is my Entity that worked:
<?php
namespace App\Models;
/**
* Books
*
* @ORM\Table(name="books")
* @ORM\Entity
*/
use Doctrine\ORM\Mapping as ORM;
class Books
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=20, nullable=true)
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="isbn", type="string", length=20, nullable=true)
*/
private $isbn;
Then in my controller:
$entityManager->find('App\Models\Books', 1)
Success!