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

≯℡__Kan透↙ 提交于 2021-01-27 17:27:52

问题


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.


回答1:


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)




回答2:


when I had this problem I added and modified the file with the new register in the next path: Resources/config/doctrine/(EntityName.orm.yml)



来源:https://stackoverflow.com/questions/20885928/doctrine-cant-generate-a-schema-when-using-single-table-inheritance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!