如何使用“类表继承”在带有 XML 映射的原则 2 中

发布于 2024-10-20 21:01:30 字数 2760 浏览 3 评论 0原文

我正在尝试将 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 学说:生成:实体“CatalogBu​​ndle”

工作正常(实体生成),但 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青春有你 2024-10-27 21:01:30

生成实体不会为您生成继承层次结构,因为这在语义上是不可能的。在调用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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文