Doctrine ORM\Table(name="name") not working

1.3k views Asked by At

My Entity class:

use Doctrine\ORM\Mapping as ORM;

/**
 * CustomerEntity
 * @ORM\Entity
 * @ORM\Table(name="customers")
 * @ORM\Table(uniqueConstraints={
 *   @ORM\UniqueConstraint(name="email", columns={"email"}),
 * })
 * @ORM\Entity(repositoryClass="Customer\V1\Rest\Customer\CustomerRepository")
 */
class CustomerEntity
{

But when I am adding a customer it throws this error, it is looking for the wrong table.

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'database.customerentity' doesn't exist

I did try with this, but doesn't help:

@ORM\Table(name="`customers`")

Schema generation shows this:

$ doctrine-module orm:schema-tool:update --dump-sql

 The following SQL statements will be executed:

 CREATE TABLE CustomerEntity (id VARCHAR(36) NOT NULL, 
 .....

What am I doing wrong?

I have cleared the cache too

orm:clear-cache:metadata
orm:clear-cache:query
orm:clear-cache:result
2

There are 2 answers

0
goulashsoup On BEST ANSWER

The problem you have is, that Doctrine expects each Annotation only once. Try this:

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Table(name="customers", uniqueConstraints={
 *   @ORM\UniqueConstraint(name="email", columns={"email"}),
 * })
 * @ORM\Entity(repositoryClass="Customer\V1\Rest\Customer\CustomerRepository")
 */
class CustomerEntity
{
0
Adarsh Khatri On

This is really frustrating, after going through ORM core codes, I was able to pinpoint the problem. Don't have time to investigate further to verify the bug or this being a feature (normal behaviour).

By inserting @ORM\Table(name="customers") below uniqueConstraints, ORM is able to recognize the table name.

use Doctrine\ORM\Mapping as ORM;

/**
 * CustomerEntity
 * @ORM\Entity
 * @ORM\Table(uniqueConstraints={
 *   @ORM\UniqueConstraint(name="email", columns={"email"}),
 * })
 * @ORM\Table(name="customers")
 * @ORM\Entity(repositoryClass="Customer\V1\Rest\Customer\CustomerRepository")
 */
class CustomerEntity
{