Doctrine:映射的键被插入为NULL

时间:2016-12-24 07:49:22

标签: doctrine mapping symfony

我有两个表,cartproduct_inventory由列sku映射。 product_inventory已将sku作为主键而非id

映射如下:

AppBundle\Entity\Cart:
    type: entity
    table: cart
    repositoryClass: AppBundle\Repository\CartRepository
    manyToOne:
      variant:
        targetEntity: ProductInventory
        inversedBy: cart
        joinColumn:
          name: sku
          referencedColumnName: sku
    id:
        id:
            type: integer
            nullable: false
            options:
                unsigned: true
            id: true
            generator:
                strategy: IDENTITY
    fields:
        userId:
            type: integer
            nullable: false
            options:
                unsigned: false
            column: user_id
        sku:
            type: string
            nullable: false
            length: 10
            options:
                fixed: false
        quantity:
            type: tinyint
            nullable: false
    lifecycleCallbacks: {  }

AppBundle\Entity\ProductInventory:
    type: entity
    table: product_inventory
    repositoryClass: AppBundle\Repository\ProductInventoryRepository
    manyToOne:
        product:
          targetEntity: Product
          inversedBy: inventory
          joinColumn:
              name: product_id
              referencedColumnName: id
    oneToMany:
        attribute_value:
          targetEntity: ProductAttributeValue
          mappedBy: inventory
          cascade: [persist]
        cart:
          targetEntity: Cart
          mappedBy: variant
    id:
        sku:
            type: string
            nullable: false
            length: 10
            options:
                fixed: false
            id: true
            generator:
                strategy: IDENTITY
    fields:
        productId:
            type: integer
            nullable: false
            options:
                unsigned: true
            column: product_id
        quantityAvailable:
            type: tinyint
            nullable: false
            column: quantity_available
        quantitySold:
            type: smallint
            nullable: false
            options:
                unsigned: true
            column: quantity_sold
    lifecycleCallbacks: {  }

我尝试将记录插入cart表,但映射的密钥sku插入为NULL。我尝试将主键更改为默认值id,但它没有用。我无法弄清楚这个问题。

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

映射不正确。它应该是相反的方式。购物车可以有很多产品(oneToMany),购物车中可以有很多产品(manyToOne)。

AppBundle\Entity\Cart:
    type: entity
    table: cart
    repositoryClass: AppBundle\Repository\CartRepository
    oneToMany:
        inventory:
          targetEntity: ProductInventory
          mappedBy: cart

AppBundle\Entity\ProductInventory:
    type: entity
    table: product_inventory
    repositoryClass: AppBundle\Repository\ProductInventoryRepository
    manyToOne:
      cart:
          targetEntity: Cart
          inversedBy: inventory
          joinColumn:
            name: sku
            referencedColumnName: sku

我感谢大家抽出时间帮助我。

答案 1 :(得分:0)

您对sku字段的定义是一个字符串,您的生成器被定义为使用IDENTITY生成器策略,这取决于使用mysql是否会将其转换为sku的AUTO_INCREMENT列属性。检查doctrine reference以了解策略类型。