【问题标题】:Notice: Undefined index: mappedBy注意:未定义索引:mappedBy
【发布时间】:2015-12-01 01:14:38
【问题描述】:

我不明白与 Doctrine2 的关系。我正在尝试遵循文档,但它缺乏正确的解释。

我有 ClientMaterialBudgetOrders 表。 Budget 表单列出了前两个表中的数据,我需要将它们与 Order 相关联,这将存储客户的 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

【问题讨论】:

    标签: symfony yaml relation


    【解决方案1】:

    YAML 语法应如下所示:

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

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

    【讨论】:

    • 谢谢,但我对关系声明后使用的名称感到迷茫,这些名称是什么?基于什么?
    • 名称(例如,cascade:、fetch: 等)是 Doctrine 在从现有数据库创建映射信息时生成的。他们可能会被忽略。重要的一点是缩进的水平。在提供的示例中,inversedBytargetEntity 具有相同的级别。您收到的错误可能是由于mappedBy 太深了一级。
    【解决方案2】:

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

    【讨论】:

    • 虽然这是真的,但错误并不是由那个特定的“不匹配”引起的,所以这根本不是一个有效的答案
    猜你喜欢
    • 2013-11-27
    • 2015-06-02
    • 2021-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-25
    • 2015-07-29
    相关资源
    最近更新 更多