Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 单继承实体的oneToMany关联_Php_Symfony_Orm_Doctrine_Sti - Fatal编程技术网

Php 单继承实体的oneToMany关联

Php 单继承实体的oneToMany关联,php,symfony,orm,doctrine,sti,Php,Symfony,Orm,Doctrine,Sti,我对ORM aka STI的单表继承映射有以下问题 从STI实体到另一个实体,似乎不可能有一个单一的关联 我有两个实体客户和供应商,具有非常相似的字段。这就是为什么我想使用条令中的单表继承选项在一个带有简单“type”列的表中获取这两个实体,以便指示行所属的实体AbstractBusiness类将作为这两个实体的基类 考虑抽象业务的实体映射 Axelvnk\CRMBundle\Entity\AbstractBusiness: type: entity table: axelvnk

我对ORM aka STI的单表继承映射有以下问题

从STI实体到另一个实体,似乎不可能有一个单一的关联

我有两个实体客户供应商,具有非常相似的字段。这就是为什么我想使用条令中的单表继承选项在一个带有简单“type”列的表中获取这两个实体,以便指示行所属的实体AbstractBusiness类将作为这两个实体的基类

考虑抽象业务的实体映射

Axelvnk\CRMBundle\Entity\AbstractBusiness:
    type: entity
    table: axelvnk_crm_business

    inheritanceType: SINGLE_TABLE

    discriminatorColumn:
        name: type
        type: string

    discriminatorMap:
        customer: Axelvnk\CRMBundle\Entity\CustomerInterface
        supplier: Axelvnk\CRMBundle\Entity\SupplierInterface

    id:
        id:
            type: guid
            generator:
                strategy: UUID

    fields:
        vatNumber:
            type: string

        label:
            type: string

    oneToOne:
        billingAddress:
            targetEntity: Axelvnk\CRMBundle\Entity\AddressInterface
            joinColumn:
                name: billing_address_id
                referencedColumnName: id
            cascade: ["all"]

    oneToMany:
        addresses:
            targetEntity: Axelvnk\CRMBundle\Entity\AddressInterface
            mappedBy: business
            cascade: ["all"]
这将是客户实体的映射

Axelvnk\CRMBundle\Entity\Customer:
    type: entity
这将是供应商实体的映射

Axelvnk\CRMBundle\Entity\Supplier:
    type: entity
目前没有其他字段。但这对这个问题没有影响

现在,AbstractBusiness::billingAddress属性引用了地址实体的实例。其映射如下所示:

Axelvnk\CRMBundle\Entity\Address:
    type: entity
    table: axelvnk_crm_address

    id:
        id:
            type: guid
            generator:
                strategy: UUID

    fields:
        streetAndNumber:
            type: string

        city:
            type: string

    manyToOne:
        business:
            targetEntity: Axelvnk\CRMBundle\Entity\AbstractBusiness
            inversedBy: addresses
            joinColumn:
                name: business_id
                referencedColumnName: id
正如您所看到的,Address::business属性引用回STI实体,即抽象业务。这可以是供应商客户,具体取决于所属方

我希望条令能够解决应该使用哪个实体类,因为它知道AbstractBusiness是一个STI实体,并且鉴别器映射已经就位。在使用地址表中的business_id查询业务表之后,它应该能够根据地址表中的“type”列计算出类,对吗?但显然不是。我不能映射回STI实体,因为doctrine不能从抽象类生成代理类,我想这是有道理的

但我现在的问题是,映射回STI实体的正确方法是什么?或者这在理论上是不可能的


谢谢你的回答

显然,在discriminatorMap中使用接口,然后使用doctrine目标实体解析器将这些接口替换为具体的类,这对于持久化实体非常有效,但要获取实体,它无法确定要使用哪个类

所以解决方案是:在应用程序中始终使用具体类


github上存在一个公开问题,即支持以与targetEntity属性相同的方式解析鉴别器映射中的类/接口:

我的项目中有到STI实体的关联映射,它们工作正常,但在我的例子中,基本实体并没有被定义为
抽象的
。。。然后它可能会尝试水合到那个级别,而这不是我想要的。不,它不应该。下面是来自真实项目的不同内容类型的鉴别器映射定义。基本实体被命名为
内容
,而不是
抽象
@ORM\DiscriminatorMap({“photo”=“photo”,“video”=“video”,“comics”=“comics”})
这很奇怪,什么版本?这是我执行此操作时遇到的异常“实体”Axelvnk\CRMBundle\Entity\Business必须是“Axelvnk\CRMBundle\Entity\Business”的鉴别器映射的一部分,才能在继承层次结构中正确映射。或者,您可以将“Axelvnk\CRMBundle\Entity\Business”设置为抽象类,以避免发生此异常。”在您的例子中,我的意思是
AbstractBusiness
不应该是鉴别器映射的一部分(它不是我看到的),也不应该是抽象类<代码>业务
和供应商
应该是从
抽象业务
继承的可实例化类。顺便说一句,您在鉴别器映射中使用的是
CustomerInterface
而不是
Customer
,可能是错误的,请尝试使用显式实体类名称