我试图找出如何正确映射我的实体,以便我可以制作Doctrine 2类表继承(man page)
所以我尝试了很多不同的解决方案,但我似乎无法使用命令行工具验证我的模型(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,我在网上找不到任何东西,因为我可能没有很好地表达它。我读到了关于关键的问题,但是当我尝试过它时,Doctrine告诉我同样的事情
No identifier/primary key specified for Entity "Api\TestBundle\Entit
y\ReferenceItemPropertyFacebook". Every Entity must have an identifier/prim
ary key.
所以,如果你有任何线索,我将不胜感激。非常感谢!
答案 0 :(得分:0)
我不熟悉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
行?对我来说似乎多余。