如何使用XML映射在Doctrine 2中使用“类表继承”

时间:2011-03-09 21:20:38

标签: doctrine-orm symfony class-table-inheritance

我正在尝试使用Doctrine 2的“类表继承功能”和XML Mapping(Symfony 2 PR 7)。

XML超类CatalogProduct的XML定义:

<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
                          http://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
    <entity name="MyApp\CatalogBundle\Entity\CatalogProduct" table="catalog_product" inheritance-type="JOINED">
        <discriminator-column name="discr" type="string" />
        <discriminator-map>
            <discriminator-mapping value="book" class="MyApp\CatalogBundle\Entity\CatalogBook" />
        </discriminator-map>
        <id name="id" type="integer" column="id">
            <generator strategy="AUTO"/>
            <sequence-generator sequence-name="tablename_seq" allocation-size="100" initial-value="1" />
        </id>
        <field name="name" column="name" type="string" length="50" nullable="true" unique="false" />
        <field name="isPublic" column="is_public" type="boolean" />        
    </entity>    
</doctrine-mapping>

XML超类CatalogBook的XML定义,它应该扩展CatalogProduct:

<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
                          http://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
    <entity name="MyApp\CatalogBundle\Entity\CatalogBook" table="catalog_book">
        <id name="id" type="integer" column="id">
            <generator strategy="AUTO"/>
            <sequence-generator sequence-name="tablename_seq" allocation-size="100" initial-value="1" />
        </id>
        <field name="author_name" column="author_name" type="string" length="50" nullable="true" unique="false" />
    </entity>    
</doctrine-mapping>

./ php app / console doctrine:generate:entities“CatalogBu​​ndle”

工作正常(实体generatetd),但CatalogBook原来是一个“简单”类,而不是扩展CatalogProduct。

$book = new CatalogBook();
$book->setAuthorName('some author');
$book->setName('some book name');

导致例外:

Fatal error: Call to undefined method MyApp\CatalogBundle\Entity\CatalogBook::setName()

我想,我在XML中遗漏了一些内容,它告诉CatalogBook实体扩展CatalogProduct。但我在Doctrine 2文档或谷歌上找不到任何有帮助的东西。

1 个答案:

答案 0 :(得分:1)

生成实体不会为您生成继承层次结构,因为这在语义上是不可能的。你必须在调用doctrine:generate:entities。

之后自己完成