Symfony 参考id上的鉴别器

Symfony 参考id上的鉴别器,symfony,doctrine-orm,symfony-2.3,Symfony,Doctrine Orm,Symfony 2.3,我的标题可能不是最好的描述,但它是我能想到的最好的描述 我有4个实体;页面、页面元素、图像和名为TextWithImage的实体。页面包含pageElements(PageElement实体数组)。这些PageElement可以有多种类型,但目前我只有一个名为TextWithImage的元素,它包含PageElement实体所包含数据之外的其他数据 PageElement可以包含在许多页面中,因此,我在PageElement.orm.yml中有许多页面。TextWithImage有一个指向Ima

我的标题可能不是最好的描述,但它是我能想到的最好的描述

我有4个实体;页面、页面元素、图像和名为TextWithImage的实体。页面包含pageElements(PageElement实体数组)。这些PageElement可以有多种类型,但目前我只有一个名为TextWithImage的元素,它包含PageElement实体所包含数据之外的其他数据

PageElement可以包含在许多页面中,因此,我在PageElement.orm.yml中有许多页面。TextWithImage有一个指向Image的多个引用

(更多信息:另一个实体ImageGallery可能与该图像实体存在多个关系,而TextOnly不应引用该图像实体。)

我希望能够获取页面并检索页面元素及其所有“属性”。假设我请求获取一个页面,其中只有一个TextWithImage类型的PageElement,我想返回以下内容

Page -> pageElements = array (
    [0] => TextWithImage -> image = Image -> filename = "image.png"
                                          -> alt = "An image!"
                         -> text  = "There's an image too!"
)
所有这些看起来都很简单,但我需要知道这个页面元素是一个TextWithImage类型。我可以用一个鉴别器列来做这件事吗,比如说(草图)

请记住,我将有不止一种类型的PageElement,而不仅仅是TextWithImage


这可能吗?如果可能,怎么可能?

我已经找到了解决问题的方法。这些是YML文件。您可以使用
php应用程序/控制台原则:generate:entities-AppBundle/Entity
生成所有实体。确保PageTextImageElement类扩展了PageElement类

Page.orm.yml

AppBundle\Entity\Page:
    type: entity
    table: null
    repositoryClass: AppBundle\Repositories\PageRepository
    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: {  }
AppBundle\Entity\PageElement:
    type: entity
    inheritanceType: SINGLE_TABLE
    discriminatorColumn:
        name: discr
        type: string
    discriminatorMap:
        pageTextImageElement: PageTextImageElement
    table: null
    repositoryClass: AppBundle\Repositories\PageElementRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        sortOrder:
            type: integer
        attributes:
            type: array
            nullable: true
    lifecycleCallbacks: {  }
AppBundle\Entity\PageTextImageElement:
    type: entity
    table: null
    oneToOne:
        image:
            targetEntity: AppBundle\Entity\Image
            joinColumn:
                name: imageId
                referencedColumnName: id
    fields:
        passage:
            type: string
            length: '255'
    lifecycleCallbacks: {  }
AppBundle\Entity\Image:
    type: entity
    table: null
    repositoryClass: AppBundle\Repositories\ImageRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        name:
            type: string
            length: '255'
            unique: true
        description:
            type: string
            length: '255'
    lifecycleCallbacks: {  }
PageElement.orm.yml

AppBundle\Entity\Page:
    type: entity
    table: null
    repositoryClass: AppBundle\Repositories\PageRepository
    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: {  }
AppBundle\Entity\PageElement:
    type: entity
    inheritanceType: SINGLE_TABLE
    discriminatorColumn:
        name: discr
        type: string
    discriminatorMap:
        pageTextImageElement: PageTextImageElement
    table: null
    repositoryClass: AppBundle\Repositories\PageElementRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        sortOrder:
            type: integer
        attributes:
            type: array
            nullable: true
    lifecycleCallbacks: {  }
AppBundle\Entity\PageTextImageElement:
    type: entity
    table: null
    oneToOne:
        image:
            targetEntity: AppBundle\Entity\Image
            joinColumn:
                name: imageId
                referencedColumnName: id
    fields:
        passage:
            type: string
            length: '255'
    lifecycleCallbacks: {  }
AppBundle\Entity\Image:
    type: entity
    table: null
    repositoryClass: AppBundle\Repositories\ImageRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        name:
            type: string
            length: '255'
            unique: true
        description:
            type: string
            length: '255'
    lifecycleCallbacks: {  }
PageTextImageElement.orm.yml

AppBundle\Entity\Page:
    type: entity
    table: null
    repositoryClass: AppBundle\Repositories\PageRepository
    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: {  }
AppBundle\Entity\PageElement:
    type: entity
    inheritanceType: SINGLE_TABLE
    discriminatorColumn:
        name: discr
        type: string
    discriminatorMap:
        pageTextImageElement: PageTextImageElement
    table: null
    repositoryClass: AppBundle\Repositories\PageElementRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        sortOrder:
            type: integer
        attributes:
            type: array
            nullable: true
    lifecycleCallbacks: {  }
AppBundle\Entity\PageTextImageElement:
    type: entity
    table: null
    oneToOne:
        image:
            targetEntity: AppBundle\Entity\Image
            joinColumn:
                name: imageId
                referencedColumnName: id
    fields:
        passage:
            type: string
            length: '255'
    lifecycleCallbacks: {  }
AppBundle\Entity\Image:
    type: entity
    table: null
    repositoryClass: AppBundle\Repositories\ImageRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        name:
            type: string
            length: '255'
            unique: true
        description:
            type: string
            length: '255'
    lifecycleCallbacks: {  }
Image.orm.yml

AppBundle\Entity\Page:
    type: entity
    table: null
    repositoryClass: AppBundle\Repositories\PageRepository
    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: {  }
AppBundle\Entity\PageElement:
    type: entity
    inheritanceType: SINGLE_TABLE
    discriminatorColumn:
        name: discr
        type: string
    discriminatorMap:
        pageTextImageElement: PageTextImageElement
    table: null
    repositoryClass: AppBundle\Repositories\PageElementRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        sortOrder:
            type: integer
        attributes:
            type: array
            nullable: true
    lifecycleCallbacks: {  }
AppBundle\Entity\PageTextImageElement:
    type: entity
    table: null
    oneToOne:
        image:
            targetEntity: AppBundle\Entity\Image
            joinColumn:
                name: imageId
                referencedColumnName: id
    fields:
        passage:
            type: string
            length: '255'
    lifecycleCallbacks: {  }
AppBundle\Entity\Image:
    type: entity
    table: null
    repositoryClass: AppBundle\Repositories\ImageRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        name:
            type: string
            length: '255'
            unique: true
        description:
            type: string
            length: '255'
    lifecycleCallbacks: {  }

我找到了解决我问题的办法。这些是YML文件。您可以使用
php应用程序/控制台原则:generate:entities-AppBundle/Entity
生成所有实体。确保PageTextImageElement类扩展了PageElement类

Page.orm.yml

AppBundle\Entity\Page:
    type: entity
    table: null
    repositoryClass: AppBundle\Repositories\PageRepository
    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: {  }
AppBundle\Entity\PageElement:
    type: entity
    inheritanceType: SINGLE_TABLE
    discriminatorColumn:
        name: discr
        type: string
    discriminatorMap:
        pageTextImageElement: PageTextImageElement
    table: null
    repositoryClass: AppBundle\Repositories\PageElementRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        sortOrder:
            type: integer
        attributes:
            type: array
            nullable: true
    lifecycleCallbacks: {  }
AppBundle\Entity\PageTextImageElement:
    type: entity
    table: null
    oneToOne:
        image:
            targetEntity: AppBundle\Entity\Image
            joinColumn:
                name: imageId
                referencedColumnName: id
    fields:
        passage:
            type: string
            length: '255'
    lifecycleCallbacks: {  }
AppBundle\Entity\Image:
    type: entity
    table: null
    repositoryClass: AppBundle\Repositories\ImageRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        name:
            type: string
            length: '255'
            unique: true
        description:
            type: string
            length: '255'
    lifecycleCallbacks: {  }
PageElement.orm.yml

AppBundle\Entity\Page:
    type: entity
    table: null
    repositoryClass: AppBundle\Repositories\PageRepository
    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: {  }
AppBundle\Entity\PageElement:
    type: entity
    inheritanceType: SINGLE_TABLE
    discriminatorColumn:
        name: discr
        type: string
    discriminatorMap:
        pageTextImageElement: PageTextImageElement
    table: null
    repositoryClass: AppBundle\Repositories\PageElementRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        sortOrder:
            type: integer
        attributes:
            type: array
            nullable: true
    lifecycleCallbacks: {  }
AppBundle\Entity\PageTextImageElement:
    type: entity
    table: null
    oneToOne:
        image:
            targetEntity: AppBundle\Entity\Image
            joinColumn:
                name: imageId
                referencedColumnName: id
    fields:
        passage:
            type: string
            length: '255'
    lifecycleCallbacks: {  }
AppBundle\Entity\Image:
    type: entity
    table: null
    repositoryClass: AppBundle\Repositories\ImageRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        name:
            type: string
            length: '255'
            unique: true
        description:
            type: string
            length: '255'
    lifecycleCallbacks: {  }
PageTextImageElement.orm.yml

AppBundle\Entity\Page:
    type: entity
    table: null
    repositoryClass: AppBundle\Repositories\PageRepository
    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: {  }
AppBundle\Entity\PageElement:
    type: entity
    inheritanceType: SINGLE_TABLE
    discriminatorColumn:
        name: discr
        type: string
    discriminatorMap:
        pageTextImageElement: PageTextImageElement
    table: null
    repositoryClass: AppBundle\Repositories\PageElementRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        sortOrder:
            type: integer
        attributes:
            type: array
            nullable: true
    lifecycleCallbacks: {  }
AppBundle\Entity\PageTextImageElement:
    type: entity
    table: null
    oneToOne:
        image:
            targetEntity: AppBundle\Entity\Image
            joinColumn:
                name: imageId
                referencedColumnName: id
    fields:
        passage:
            type: string
            length: '255'
    lifecycleCallbacks: {  }
AppBundle\Entity\Image:
    type: entity
    table: null
    repositoryClass: AppBundle\Repositories\ImageRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        name:
            type: string
            length: '255'
            unique: true
        description:
            type: string
            length: '255'
    lifecycleCallbacks: {  }
Image.orm.yml

AppBundle\Entity\Page:
    type: entity
    table: null
    repositoryClass: AppBundle\Repositories\PageRepository
    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: {  }
AppBundle\Entity\PageElement:
    type: entity
    inheritanceType: SINGLE_TABLE
    discriminatorColumn:
        name: discr
        type: string
    discriminatorMap:
        pageTextImageElement: PageTextImageElement
    table: null
    repositoryClass: AppBundle\Repositories\PageElementRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        sortOrder:
            type: integer
        attributes:
            type: array
            nullable: true
    lifecycleCallbacks: {  }
AppBundle\Entity\PageTextImageElement:
    type: entity
    table: null
    oneToOne:
        image:
            targetEntity: AppBundle\Entity\Image
            joinColumn:
                name: imageId
                referencedColumnName: id
    fields:
        passage:
            type: string
            length: '255'
    lifecycleCallbacks: {  }
AppBundle\Entity\Image:
    type: entity
    table: null
    repositoryClass: AppBundle\Repositories\ImageRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        name:
            type: string
            length: '255'
            unique: true
        description:
            type: string
            length: '255'
    lifecycleCallbacks: {  }