如何使用“类表继承”在带有 XML 映射的原则 2 中
我正在尝试将 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 app/console 学说:生成:实体“CatalogBundle”
工作正常(实体生成),但 CatalogBook 结果是一个“ simple”类,不扩展 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 上找不到任何有帮助的内容。
I'm trying to use the "class table inheritance feature" of Doctrine 2 with XML Mapping (Symfony 2 PR 7).
XML definition of 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
生成实体不会为您生成继承层次结构,因为这在语义上是不可能的。在调用doctrine:generate:entities后你必须自己做。
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.