douzhuolong9886 2012-03-27 16:31
浏览 39
已采纳

Doctrine 2关联继承与不同的类

I'm using Doctrine 2 and single table inheritance, my topmost entity hierarchy is:

JS_File --> [one to many] --> JS_File_Version

And then my sub-class entity hierarchy is:

JS_Image (extends JS_File) --> [one to many] --> JS_Image_Version (extends JS_File_Version)

The inheritance mapping works perfectly for JS_File and JS_Image, and the association mapping works perfectly for JS_File --> JS_File_Version. But, when it comes to map the "versions" association on JS_Image I get:

'Property "versions" in "JS_Image" was already declared, but it must be declared only once'

I understand why this is, the association is being inherited from JS_File and I can't overwrite it, but how do I achieve what I'm trying to do? Basically, with inheritance, how can I tell Doctrine to use a different targetEntity for the sub-classes association.

  • 写回答

2条回答 默认 最新

  • duanji8887 2012-03-29 09:38
    关注

    Looks like what I'm trying to do isn't possible (having both JS_File and JS_Image as concrete entities). I've followed the advice of someone from the doctrine-user mailing list instead:

    The way I solved this was to use an abstract base class and then define the actual mappings on the subclasses. For example, you could do something like:

    JS_AbstractFile with subclasses: JS_File and JS_Image JS_AbstractVersion with subclasses: JS_FileVersion and JS_ImageVersion

    AbstractFile and AbstractVersion can define the property (and getter/setters) but you shouldn't define any mapping information for the field. Then in your subclass you re-define the property along with mapping information (at least, thats how I did it since I used annotations)

    http://groups.google.com/group/doctrine-user/msg/243f355191d3a512

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化