本文介绍了Doctrine2:OneToMany映射超类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库结构如下:



工作:




  • a href =http://docs.doctrine-project.org/en/latest/reference/inheritance-mapping.html#class-table-继承> CTI 表工作

  • AbstractImageWork ,其扩展工作

  • 决赛桌 PhotoWork / li>


评论:




  • 评论

  • 决赛桌 WorkComment ,其扩展评论



WorkComment 具有与工作的ManyToOne关系:

  @ManyToOne(targetEntity =工作,inversedBy =评论)

工作 WorkComment 有一个OneToMany关系:

 code> @OneToMany(targetEntity =WorkComment,mappedBy =work)

问题是,在更新架构时,教义给我这个错误:

  [Doctrine\ORM\Mapping\MappingException] 

映射的超类Acme\ ... \AbstractImageWork#comments上放置一对多或多对多关联是非法的。

我想这与MappedSuperclass AbstractImageWork 有关在工作 PhotoWork 之间的中间,但我并没有把这个关系放在MappedSuperclass上,而是在CTI表上,所以为什么Doctrine会这样做? p>

任何想法?

解决方案

在某些情况下,当你有这样的错误当继承自被标记为 @ ORM\MappedSuperclass 的类的类时,尝试将您的属性访问级别从私有更改为 protected


My DB structure is as follows:

work:

  • CTI table Work
  • MappedSuperclass table AbstractImageWork which extends Work
  • final table PhotoWork which extends AbstractImageWork

comment:

WorkComment has a ManyToOne relation to Work:

@ManyToOne(targetEntity="Work", inversedBy="comments")

Work has a OneToMany relation to WorkComment:

@OneToMany(targetEntity="WorkComment", mappedBy="work")

The problem is that Doctrine gives me this error while updating the schema:

[Doctrine\ORM\Mapping\MappingException]
It is illegal to put an inverse side one-to-many or many-to-many association on
mapped superclass 'Acme\...\AbstractImageWork#comments'.

I guess this has something to do with the MappedSuperclass AbstractImageWork stuck in the middle between Work and PhotoWork, but I didn't actually put this relation on the MappedSuperclass, but on the CTI table.. so why will Doctrine behave like this?

Any ideas?

解决方案

In some cases, when you have such error when inherit from the class that is tagged as @ORM\MappedSuperclass, try to change your properties access level from private to protected

这篇关于Doctrine2:OneToMany映射超类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-13 23:56