How to use "class table inheritance" in Doctrine 2 with XML mapping
I'm trying to use the "class table inheritance feature" of Doctrine 2 with XML Mapping (Symfony 2 PR 7).
XML definition of 开发者_Go百科the XML superclass 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\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 definition of the XML superclass CatalogBook, that should extend 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 "CatalogBundle"
works fine (entities generatetd), but CatalogBook turns out to be a "simple" class, not extending CatalogProduct.
$book = new CatalogBook();
$book->setAuthorName('some author');
$book->setName('some book name');
leads to an exception:
Fatal error: Call to undefined method MyApp\CatalogBundle\Entity\CatalogBook::setName()
I guess, I'm missing something in the XML, that tells the CatalogBook Entity to extend CatalogProduct. But I coudln't find anything in the Doctrine 2 Documentation or on Google that helped.
Generate entities does not generate the inheritance hierachy for you, because that is semantically not possible. You have to do it yourself after invoking doctrine:generate:entities.
精彩评论