我的标题可能不是最好的描述,但它是我能想到的最好的描述。
我有4个实体;Page、PageElement、Image和一个名为TextWithImage的实体。页面包含PageElement(PageElement实体的数组)。这些PageElement可以有多种类型,但目前我只有一个名为TextWithImage的类型,它保存了PageElement实体保存的数据之外的其他数据。
PageElement可以包含在许多页面上,因此,我在PageElement.org.myml中有一个ManyToMany。TextWithImage有一个manyToOne可以引用Image。
(更多信息:另一个实体ImageGallery可能与Image实体有很多关系,而TextOnly不应该引用Image实体。)
我希望能够获得页面并检索具有所有"属性"的PageElements。假设我请求获得一个只有一种PageElement的TextWithImage类型的页面,我想返回以下内容。
Page -> pageElements = array (
[0] => TextWithImage -> image = Image -> filename = "image.png"
-> alt = "An image!"
-> text = "There's an image too!"
)
所有这些看起来都很简单,但我需要原则来理解这个PageElement是一个TextWithImage类型。我可以用鉴别器柱来做这件事吗,比如(粗略草图);
Table: pageelement
id | attributes | discr | TextWithImageId
Table: textwithimage
id | attributes
请记住,我将拥有不止一种类型的PageElement,而不仅仅是TextWithImage。
这可能吗?如果可能,怎么做?
我找到了问题的解决方案。这些是基督教青年会的教义文件。可以使用php app/console doctrine:generate:entities AppBundle/Entity
生成所有实体确保PageTextImageElement类扩展了PageElement类
Page.orm.yml
AppBundleEntityPage:
type: entity
table: null
repositoryClass: AppBundleRepositoriesPageRepository
manyToMany:
pageElements:
targetEntity: PageElement
cascade: ["all"]
joinTable:
name: null
joinColumns:
page_id:
referencedColumnName: id
onDelete: CASCADE
inverseJoinColumns:
page_element_id:
referencedColumnName: id
unique: true
onDelete: CASCADE
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
name:
type: string
length: '255'
unique: true
lifecycleCallbacks: { }
PageElement.orm.yml
AppBundleEntityPageElement:
type: entity
inheritanceType: SINGLE_TABLE
discriminatorColumn:
name: discr
type: string
discriminatorMap:
pageTextImageElement: PageTextImageElement
table: null
repositoryClass: AppBundleRepositoriesPageElementRepository
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
sortOrder:
type: integer
attributes:
type: array
nullable: true
lifecycleCallbacks: { }
PageTextImageElement.orm.yml
AppBundleEntityPageTextImageElement:
type: entity
table: null
oneToOne:
image:
targetEntity: AppBundleEntityImage
joinColumn:
name: imageId
referencedColumnName: id
fields:
passage:
type: string
length: '255'
lifecycleCallbacks: { }
图像.orm.yml
AppBundleEntityImage:
type: entity
table: null
repositoryClass: AppBundleRepositoriesImageRepository
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
name:
type: string
length: '255'
unique: true
description:
type: string
length: '255'
lifecycleCallbacks: { }