简体   繁体   中英

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 (ie 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.

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)

当我遇到这个问题时,我在下一个路径中添加并修改了带有新寄存器的文件:Resources / config / doctrine /(EntityName.orm.yml)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM