【问题标题】:Doctrine 2 class heritance mappingDoctrine 2 类继承映射
【发布时间】:2015-07-01 18:16:20
【问题描述】:

我正在尝试弄清楚如何正确映射我的实体,以便我可以进行 Doctrine 2 类表继承 (Doc here)

所以我尝试了许多不同的解决方案,但我似乎无法使用命令行工具 (doctrine:schema:validate) 验证我的模型

这就是我所尝试的。我还没有更改父类,所以我将其留在这里。

Api\TestBundle\Entity\Item:
type: entity
table: items
inheritanceType: JOINED
discriminatorColumn:
  name: type
  type: string
  nullable: false
  length: 32
  fixed: false
  comment: ''
discriminatorMap:
    item_property_facebook: ItemPropertyFacebook
id:
    id:
        type: integer
        nullable: false
        unsigned: false
        comment: ''
        id: true
        generator:
            strategy: AUTO
fields:
    bundledType:
        type: string
        nullable: false
        length: 32
        fixed: false
        comment: ''
        column: bundled_type
lifecycleCallbacks: {  }

对于子类,我先放了另一个id:

Api\TestBundle\Entity\ItemPropertyFacebook:
type: entity
table: item_property_facebook
id:
    id:
        type: integer
        nullable: false
        unsigned: false
        comment: ''
        id: true
        generator:
            strategy: AUTO
fields:
    account_id:
        type: integer
lifecycleCallbacks: {  }

显然,我明白了:

Duplicate definition of column 'id' on entity 'Api\TestBundle\Entity
\ItemPropertyFacebook' in a field or discriminator column mapping.

所以我尝试删除孩子中的 id 定义,我得到:

No identifier/primary key specified for Entity "Api\TestBundle\Entit
y\ReferenceItemPropertyFacebook". Every Entity must have an identifier/prim
ary key.

所以我完全不知道。在文档中,他们只设置了基本的东西,但没有设置 id,我在网上找不到任何东西,因为我可能没有很好地制定它。我读到了 associationKey,但是当我尝试过时,Doctrine 告诉我同样的情况

No identifier/primary key specified for Entity "Api\TestBundle\Entit
y\ReferenceItemPropertyFacebook". Every Entity must have an identifier/prim
ary key.

如果您有任何线索,我将不胜感激。非常感谢!

【问题讨论】:

  • 您是否在您的 ItemPropertyFacebook 类中扩展了 Item 类?无论如何,您必须删除子类中的 id 属性。
  • 是的,我的 ItemPropertyFacebook 类扩展了 Item

标签: php symfony doctrine-orm doctrine yaml


【解决方案1】:

我对 yaml 生成不熟悉,但是当我尝试使用 php 注释的结构时,它对我有用,重复的 id 列没有问题:

/**
 * @ORM\Entity
 * @ORM\Table(name="items")
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="type", type="string")
 * @ORM\DiscriminatorMap({"item" = "Item", "item_property_facebook" = "ItemPropertyFacebook"})
 */
class Item {

    /**
     * @ORM\Column
     * @ORM\Id
     * @ORM\GeneratedValue()
     */
    protected $id;

    /**
     * @ORM\Column(type="string")
     */
    protected $bundledType;
}

/**
 * @ORM\Entity
 * @ORM\Table(name="item_property_facebook")
 */
class ItemPropertyFacebook extends Item {

    /**
     * @var
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue()
     */
    protected $id;

    /**
     * @ORM\Column(type="integer")
     */
    protected $account_id;
}

将生成那些 sql 命令:

CREATE TABLE items (id VARCHAR(255) NOT NULL, bundledType VARCHAR(255) NOT NULL, type VARCHAR(255) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
CREATE TABLE item_property_facebook (id VARCHAR(255) NOT NULL, account_id INT NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
ALTER TABLE item_property_facebook ADD CONSTRAINT FK_B8C39352BF396750 FOREIGN KEY (id) REFERENCES items (id) ON DELETE CASCADE

唯一的区别是,我必须将 "item = "Item" 添加到 DiscriminatorMap。

另外,id 定义中应该有id: true 行吗?对我来说似乎是多余的。

【讨论】:

  • 我想知道为什么在文档中,这是唯一没有 yaml 示例的继承类型。也许他们应该添加它,我会尝试并将您的回复标记为答案,如果它有效。我也会写信给他们,以便他们在文档中添加注释。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多