2015-09-04 84 views
2

我不理解与Doctrine2的关系。我正在尝试按照文档,但它缺乏适当的解释。注意:未定义索引:mappedBy

客户材料预算订单表。 预算形式列出了前两个表中的数据,我需要将它们都与订购相关联,这将存储客户端的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个表格的最后五个数据,并且我得到这个错误:

Notice: Undefined index: mappedBy

回答

1

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

+0

虽然这是真的,但错误并非由于特定的“不匹配”而导致的,所以这根本就不是一个有效的答案 – DonCallisto

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 

注意,人们通常会想到那里是客户与订单之间的多对多关系,除非客户将有且只有一个数量级。

+0

谢谢,但我感觉在关系声明后使用的名字中丢失了,这些名字是什么?基于什么? – mfgabriel92

+1

当从现有数据库创建映射信息时,由Doctrine生成名称(例如,cascade :, fetch:等)。他们可能会被忽略。重要的一点是缩进的级别。在提供的示例中,'inversedBy'与'targetEntity'具有相同的等级。您收到的错误可能是由于'mappedBy'是一个太深的水平​​。 – geoB

+0

@geoB:你应该解释错误在哪里...... – DonCallisto