Symfony 2 / Doctrine不加载关联实体

时间:2014-08-07 08:48:56

标签: php symfony doctrine-orm doctrine

我在学说和延迟加载一对多/多对一双向绑定方面存在问题。

Folowing Scenario(仅限必要部分):

两个表:

Configuration:
type: entity
table: configuration
id:
    idconfiguration:
        type: integer
        nullable: false
        unsigned: false
        comment: ''
        id: true
        generator:
            strategy: IDENTITY
fields:
    fkbasemodel:
        type: integer
        nullable: false
        unsigned: false
        comment: ''
manyToOne:
  basemodel:
    targetEntity: Basemodel
    inversedBy: configurations
    joinColumn:
      name: fkbasemodel
      referencedColumnName: idbasemodel

配置yaml的完整代码:     http://pastebin.com/v0G8TYqQ

Basemodel:
    type: entity
    table: basemodel
    id:
        idbasemodel:
            type: integer
            nullable: false
            unsigned: false
            comment: ''
            id: true
            generator:
                strategy: IDENTITY
    oneToMany:
        configurations:
            targetEntity: Configuration
            mappedBy: basemodel

Basemodel yaml的完整代码:     http://pastebin.com/2aBRNF1g

Folowing DB Entries:

Basemodel-表:

+-------------+
| idbasemodel |
+-------------+
|           1 |
|           2 |
+-------------+

配置 - 表:

+-----------------+-------------+
| idconfiguration | fkbasemodel |
+-----------------+-------------+
|               1 |           1 |
|               2 |           1 |
+-----------------+-------------+

配置类:     http://pastebin.com/sWYgRpjr

Basemodel类:     http://pastebin.com/yaiD8kCB

当我获取配置实体并调用' getBasemodel()'时。它总是返回null。即使' getFkbasemodel()'返回正确的外键。

为什么doesent会解析为正确的Basemodel实体?它与其他协会的工作方式相同。

编辑:

为完整代码添加了Pastbin链接

1 个答案:

答案 0 :(得分:1)

在配置的完整版本中,您有oneToMany / manyToOne的多个条目。不要多次指定该键,而应将该类型的所有关系放在一个中。有这样的重复项使得yaml选择最后一个作为唯一的一个(它们是映射(字典),因此每个键只能有一个条目 - 还要注意,虽然最后一个条目的选择是发生的,但这不是定义的行为)

你有:

oneToMany:
    configurations:
        targetEntity: Configuration
        mappedBy: basemodel
oneToMany:
    offers:
        targetEntity: Offer
        mappedBy: basemodel

应该是:

oneToMany:
    configurations:
        targetEntity: Configuration
        mappedBy: basemodel
    offers:
        targetEntity: Offer
        mappedBy: basemodel