【问题标题】:how do I get Doctrine or Symfony to generate the `extends` statement如何让 Doctrine 或 Symfony 生成“扩展”语句
【发布时间】:2016-03-13 21:03:34
【问题描述】:

我使用 Symfony 2.8.3。使用 Doctrine ORM 2.4.8。 我使用 YAML 来定义我的数据模型。

我有一个像这样的映射超类:

    AppBundle\Entity\Location:
        type: mappedSuperclass
        fields:
            street:
                type: string
                length: '100'

我从这个超类派生如下:

    AppBundle\Entity\Building:
        type: entity
        extends: AppBundle\Entity\Location
        id:
            id:
                type: integer
                id: true
                generator:
                    strategy: AUTO

我现在希望建筑物同时具有 id 和街道属性。

使用app/console doctrine:generate:entities AppBundle 这可以让我得到预期的 PHP 文件。然后使用app/console doctrine:schema:update --force 我得到一个building 数据库表,但它只有一个id 列,没有street 列。

只有在我将Building.php 更改为

    class Building

    class Building extends Location

然后重新运行app/console doctrine:schema:update --force,我得到building 表中的street 列。

我希望 Doctrine(或 Symfony)为我生成这个 extends 语句,因为我在 de YAML 源代码中指定了一个 extends

我做错了什么?

或者,我如何让 Doctrine 或 Symfony 生成 extends 语句?

【问题讨论】:

    标签: symfony doctrine-orm


    【解决方案1】:

    对此的简短回答:Doctrine 的 ORM(以及扩展的 Symfony 包)不会为您生成 extends 语句,这是您必须手动添加和维护的内容。这样做的原因主要是因为 Doctrine ORM 作为一个项目不想支持临时代码生成。查看 this GitHub issues thread 关于 Doctrine 中 PSR-4 支持的信息,了解项目维护者对此的立场。

    Doctrine 为您生成的实体是您项目的业务/领域对象,因此由您负责维护它们,在这种情况下,强制执行您的类层次结构。

    或者,没有什么能阻止您在支持您的用例的 Doctrine 映射之上扩展/创建自己的代码生成器。

    【讨论】:

    • 谢谢。那不是我:)
    猜你喜欢
    • 2017-01-01
    • 2021-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-15
    • 2016-11-23
    • 2011-04-02
    相关资源
    最近更新 更多