ORM Mapping error in Doctrine 2

Asked

Viewed 275 times

1

I’m mapping the database with Doctrine and I’m having a problem. When I use the Annotations as follows it works perfectly:

/**
 * @Entity
 * @Table(name="customer",uniqueConstraints={@UniqueConstraint(name="email", columns={"email"})})
 */
class Customer {
    (atributos)...
}

Well, I want to use the use Doctrine\ORM\Mapping AS ORM; so that the annotations start with ORM\ Example:

use Doctrine\ORM\Mapping AS ORM;
/**
 * @ORM\Entity
 * @ORM\Table(name="customer",uniqueConstraints={@ORM\UniqueConstraint(name="email", columns={"email"})})
 */
class Customer {
    (atributos)...
}

But in this way Doctrine is not interpreting the class as a valid entity...

Fatal error: Uncaught exception 'Doctrine\ORM\Mapping\MappingException' with message 'Class "Customer" is not a valid entity or mapped super class.'

My bootstrap.php is as follows

require_once "vendor/autoload.php";

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

$entidades = array("entity/");
$isDevMode = true;

// configurações de conexão.
$dbParams = array(
'driver'   => 'pdo_mysql',
'host'     => 'localhost',
'user'     => 'root',
'password' => 'pass',
'dbname'   => 'LojaVirtual',
'charset'  => 'UTF8'
);    
//setando as configurações definidas anteriormente
$config = Setup::createAnnotationMetadataConfiguration($entidades, $isDevMode);   
//criando o Entity Manager com base nas configurações de dev e banco de dados
$entityManager = EntityManager::create($dbParams, $config);

Does anyone know how to solve this problem?

  • How is the namespace organization of your project? I believe that for some reason Doctrine may be looking for the class ORM\Entity within the namespaces of its entities.

  • I really don’t know if this is it, because this mistake is common when I forget to declare the @Entity in the first case, already the namespaces he is recognizing normally, the autoload is the way that the Composer created pro Doctrine and I only acquired the classmap of the entities... It has recognized all classes normally when it comes to bootstrap.php or the test.php file

  • @Rodrigorigotti no new idea?

  • can host your code somewhere so I can test it?

  • I’ll pass the project to git

  • @Rodrigorigotti u.u a part of the project is not giving to publish, as soon as I get notice

  • @RodrigoRigotti https://github.com/rodrigoborth/LojaVirtual2

  • I’ve already been able to reproduce. I’m testing.

Show 3 more comments

1 answer

2


I was able to solve by setting the last parameter ($useSimpleReaderAnnotation) of the call createAnnotationMetadataConfiguration as false:

$config = Setup::createAnnotationMetadataConfiguration($entidades, $isDevMode, null, null, false);

The point is that when this parameter is true (which is its default value), Doctrine does not understand when annotations classes are prefixed with @ORM and failure to gather information from entities.

That question helps to understand a little the problem.

  • Man, thank you very much, solved the problem. I was killing myself to solve it a few days ago already.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.