Doctrine2关联映射'ManyToMany'错过了'targetEntity'属性。例外

时间:2014-05-15 20:03:47

标签: symfony orm doctrine mapping

我想创建三个表使用symfony2和doctrine。公司,类别和类别相关。这两个表有很多关系,我将这个关系保存在categoryRelation表中。我的yml映射是;

Firm:
// other columns
  ManyToMany:
    categories:
      targetEntity: Category
      joinTable:
        name: category_relation
        joinColumns:
          firm_id:
            referencedColumnName: id
        inverseJoinColumns:
          category_id:
            referencedColumnName: id


Category:
 // other columns
  manyToMany:
    firms:
    targetEntity: Firm
    mappedBy: categories

并给我这个错误

The association mapping 'ManyToMany' misses the 'targetEntity' attribute. 

如何解决此错误?谢谢你的回答

1 个答案:

答案 0 :(得分:2)

我将您的代码与http://docs.doctrine-project.org/en/2.0.x/reference/association-mapping.html的示例代码进行了比较,第5.14节。多对多,双向。我粘贴下面的代码:

User:
  type: entity
  manyToMany:
    groups:
      targetEntity: Group
      inversedBy: users
      joinTable:
        name: users_groups
        joinColumns:
          user_id:
            referencedColumnName: id
        inverseJoinColumns:
          group_id:
            referencedColumnName: id

Group:
  type: entity
  manyToMany:
    users:
      targetEntity: User
      mappedBy: groups

我已经看到了一些可能产生异常的差异。首先,您没有编写类型:entity指令。你的第一个manyToMany指令有大写字母的第一个M.反转的指令不存在。最后我认为这是主要问题,你的指令targetEntity和mappedBy没有缩进:

firms:
targetEntity: Firm
mappedBy: categories

并且YAML基于缩进,因此,可能这是Exception中指定的targetEntity。你应该这样写:

firms:
    targetEntity: Firm
    mappedBy: categories