注意:未定义的索引:mappedBy

时间:2015-09-04 19:46:07

标签: symfony yaml relation

我不理解与Doctrine2的关系。我正在尝试按照文档进行操作,但缺乏正确的解释。

我有客户材料预算订单表。 预算表单列出了前两个表中的数据,我需要将它们与订单相关联,这将存储客户端的ID以及所需的所有材料ID。因此,按照文档,我在Orders.orm.yml

中得出了这个结果
CDG\PanelBundle\Entity\Orders:
    type: entity
    table: orders
    repositoryClass: CDG\PanelBundle\Entity\OrdersRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        materialQuantity:
            type: integer
            column: material_quantity
        materialPrice:
            type: decimal
            column: material_price
        dateCreated:
            type: datetime
            column: date_created
    oneToOne:
        clientId:
            targetEntity: Client
            joinColumn:
                name: client_id
                referencedToColumnName: id
        order:
            targetEntity: Orders
            joinColumn:
                name: order
                referencedToColumnName: id
    manyToOne:
        materialId:
            targetEntity: Material
            joinColumn:
                name: material_id
                mappedBy: materials
    lifecycleCallbacks: {  }

我的Material.orm.yml

CDG\PanelBundle\Entity\Material:
    type: entity
    table: material
    repositoryClass: CDG\PanelBundle\Entity\MaterialRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        name:
            type: string
            column: name
            length: 255
        description:
            type: string
            column: description
            length: 255
        quantity:
            type: integer
            column: quantity
        price:
            type: integer
            column: price
    oneToMany:
        materials:
            targetEntity: 
    lifecycleCallbacks: {  }

Budget.orm.yml

CDG\PanelBundle\Entity\Budget:
    type: entity
    table: null
    repositoryClass: CDG\PanelBundle\Entity\BudgetRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        clientName:
            type: string
            length: 255
            column: client_name
        materials:
            type: array
        address:
            type: string
            length: 255
        installments:
            type: integer
        checkDays:
            type: integer
            column: check_days
        totalValue:
            type: decimal
            column: total_value
        order:
            type: integer
    lifecycleCallbacks: {  }

在我的主页中,我列出了所有3个表中的最后五个数据,我收到此错误:

  

注意:未定义索引:mappedBy

2 个答案:

答案 0 :(得分:1)

oneToMany 关系中,您应该使用mappedBy选项。在 manyToOne 关系中,您使用inversedBy选项。

答案 1 :(得分:0)

YAML语法应如下所示:

table: orders
repositoryClass: CDG\PanelBundle\Entity\OrdersRepository
...
    manyToOne:
        materialId:
            targetEntity: Material
            cascade: {  }
            fetch: LAZY
            inversedBy: null
            joinColumns:
                material_id:
                    referencedColumnName: id
            orphanRemoval: false

请注意,除非客户只有一个且只有一个订单,否则通常会在客户和订单之间存在ManyToMany关系。