Symfony2>教义> double oneToMany组合键

时间:2014-08-05 20:16:10

标签: symfony orm doctrine-orm yaml one-to-many

我试图设置'产品'之间的关系。和'商人'通过链接表' MerchantProduct'我可以存储/覆盖其他信息。因此主键由组合键组成。我试图简化每个实体的字段数量以增强可读性。在我尝试在YML中添加关系之前,这三个实体已经正常工作了,但我想一直走到'与学说。

我得到的错误是:

  

[学说\ ORM \ ORMException]
  从中引用的列名id   BLAAT \ Bundle \ AdminBundle \ Entity \ Core \ Product to   BLAAT \ Bundle \ AdminBundle \ Entity \ Core \ MerchantProduct不存在。

我不确定为什么会收到此错误并且不知道如何解决。我担心它与组合的主键有关。

使用额外覆盖信息链接实体

BLAAT\Bundle\AdminBundle\Entity\Core\MerchantProduct:
    type: entity
    table: MerchantProduct
    repositoryClass: BLAAT\Bundle\AdminBundle\Entity\Core\MerchantProductRepository
    id:
        merchantId:
            type: integer
            column: merchant_id
        productId:
            type: string
            length: 255
            column: product_id

    fields:
        inStock:
            type: boolean
            column: in_stock
            nullable: TRUE
        productPrice:
            type: float
            column: product_price
            nullable: TRUE
        productDescription:
            type: text
            column: product_description
            nullable: TRUE

    oneToMany:
        products:
            targetEntity: Product
            mappedBy: product_id

    oneToMany:
        outlets:
            targetEntity: Merchant
            mappedBy: merchant_id

    lifecycleCallbacks: {  }

产品实体

BLAAT\Bundle\AdminBundle\Entity\Core\Product:
    type: entity
    table: Product
    repositoryClass: BLAAT\Bundle\AdminBundle\Entity\Core\ProductRepository
    id:
        id:
            type: string
            length: 255
            id: true
            generator:
                strategy: NONE
    fields:
        title:
            type: string
            length: 255
        defaultDescription:
            type: text
            column: default_description
            nullable: TRUE
        defaultPrice:
            type: float
            column: default_price
            nullable: TRUE

    manyToOne:
        sellers:
            targetEntity: MerchantProduct
            inversedBy: products
            joinColumn:
                name: product_id
                referencedColumnName: id

    lifecycleCallbacks: {  }

商户实体

BLAAT\Bundle\AdminBundle\Entity\Core\Merchant:
    type: entity
    table: null
    repositoryClass: BLAAT\Bundle\AdminBundle\Entity\Core\MerchantRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        title:
            type: string
            length: 255
        address:
            type: string
            length: 255

    manyToOne:
        stock:
            targetEntity: MerchantProduct
            inversedBy: outlets
            joinColumn:
                name: merchant_id
                referencedColumnName: id

    lifecycleCallbacks: {  }

1 个答案:

答案 0 :(得分:0)

我认为MerchantProduct中的mappedBy应该是sellersstock,而referencedColumnName应该是productIdmerchantId(或者可能是product_idmerchant_id - 我还没有对它进行过测试,但无论如何都存在于MerchantProduct中。

后者似乎导致错误,因为没有列id:它应该是productId

要处理复合主键,我认为您需要使用多个JoinColumn来定义商家和产品中的manyToOne关系。如果我正确阅读文档,您可以在JoinColumns下合并多个JoinColumn

所以:

manyToOne:
    stock:
        targetEntity: MerchantProduct
        inversedBy: outlets
        joinColumns:
            joinColumn:
                name: merchant_id
                referencedColumnName: merchantId
            joinColumn:
                name: product_id
                referencedColumnName: productId