我正在尝试将 Doctrine 2 的“类表继承功能”与 XML 映射(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 应用程序/控制台原则:生成:实体“CatalogBundle”
工作正常(实体生成),但 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 文档或 Google 上找不到任何有帮助的东西。