Jez Jez - 3 months ago 17
PHP Question

Doctrine can't generate a schema when using single table inheritance?

An interesting problem. I'm building an application using Symfony 2 and I'm trying to get Doctrine to auto-generate my schema from my entity classes. I have two entity classes,

Invoice
and
CreditNote
.
CreditNote
extends
Invoice
. They look like this -

Invoice.php -

namespace My\Bundle\BillingBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
* @ORM\Table(name="invoice")
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="discriminator", type="string")
* @ORM\DiscriminatorMap({"invoice" = "Invoice", "credit_note" = "CreditNote"})
*/
class Invoice
{ ... }


and CreditNote.php -

namespace My\Bundle\BillingBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
*/
class CreditNote extends Invoice
{ ... }


...all of which seems to follow Doctrine's guidance on single table inheritance at http://docs.doctrine-project.org/en/2.0.x/reference/inheritance-mapping.html#single-table-inheritance.

But, when I try to run
php app/console doctrine:database:create
in order to generate my schema I get the error -

PHP Fatal error: Cannot redeclare class My\Bundle\BillingBundle\Entity\Invoice in <my-app>/src/My/Bundle/BillingBundle/Entity/Invoice.php on line 15


Which suggests that the
Invoice
class has already been declared (but it honestly hasn't). More interesting is that if I rename the
Invoice
class to
AInvoice
(i.e. something that comes before
CreditNote
in the alphabet, then everything works.

So, it seems like the generator is loading up
CreditNote.php
, finding a class that extends
Invoice
and then loading
Invoice.php
, then finding the
Invoice.php
file again and trying to parse it a second time. Only that seems the kind of thing that someone would have fixed by now.

Any thoughts? Thanks.

Answer

it is 100% working code - I tried just

You may need just to clean the cache using:

php app/console cache:clear && php app/console cache:clear --env=prod

but I use php app/console doctrine:schema:update (not doctrine:database:create)