与教义建立一对多的多态关系 [英] Creating a one to many polymorphic relationship with doctrine

查看:105
本文介绍了与教义建立一对多的多态关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我先来概述一下场景。我有一个可以分配给许多不同对象的Notes对象




  • 可以有一个或多个<强>注意秒。

  • 图片可以有一个或多个注意

  • A 地址可以有一个或多个注意



我想像的是数据库如:



  id |标题|页
1 |哈利波特200
2 |宝座游戏| 500

图像

  id |满| thumb 
2 | image.jpg | image-thumb.jpg

地址

  id |街道| city 
1 | 123街|丹佛
2 |中央大道tampa

备注

  id | object_id | object_type |内容| date 
1 | 1 |图像| 可爱的图片| 2015-02-10
2 | 1 |图像| 红色色调| 2015-02-30
3 | 1 |地址| 无效| 2015-01-05
4 | 2 |书| 笨蛋| 2014-09-06
5 | 1 |书| prettygood|问题是如何在Doctrine内建模这个问题。关于单表继承的一切,我没有一对多的关系。



要注意(不是双关意图的),您可能会注意到该笔记根本不需要基于相关对象的唯一属性。



理想情况下,我可以执行 $ address-> getNotes(),这将返回相同类型的Note对象 $ image-> getNotes()将返回。



我至少要解决的问题是避免使用三个不同的表格:image_notes,book_notes和address_notes。

解决方案

这个问题向应用程序中引入了不必要的复杂性。只是因为笔记具有相同的结构并不意味着它们是相同的实体。当在3NF中对数据库进行建模时,它们不是相同的实体,因为注释不能从书籍移动到地址。在你的描述中,书和book_note之间有一个明确的亲子关系,所以建立它。



更多表不是数据库的问题,但不必要的代码复杂性就是这个问题所示。它只是聪明的聪明的缘故。这是ORM的麻烦,人们停止进行完整的归一化,不要正确地建立数据库。


Let me start by outlining the scenario. I have a Note object that can be assigned to many different objects

  • A Book can have one or moreNotes.
  • An Image can have one or more Notes.
  • A Address can have one or more Notes.

What I envision the database to look like:

book

id | title           | pages
1  | harry potter    | 200
2  | game of thrones | 500

image

id | full      | thumb
2  | image.jpg | image-thumb.jpg

address

id | street      | city
1  | 123 street  | denver
2  | central ave | tampa

note

id | object_id | object_type | content      | date
1  | 1         | image       | "lovely pic" | 2015-02-10
2  | 1         | image       | "red tint"   | 2015-02-30
3  | 1         | address     | "invalid"    | 2015-01-05
4  | 2         | book        | "boobies"    | 2014-09-06
5  | 1         | book        | "prettygood" | 2016-05-05

The question is, how do I model this inside of Doctrine. Everything I have read about talks about single table inheritance without a one to many relationship.

To note (no pun intended ;), you may notice that note never needs unique attributes based on the object its related to.

Ideally I can do $address->getNotes() which would return the same type of Note object that $image->getNotes() would return.

At the very minimum the problem I am trying to solve is to avoid having three different tables: image_notes, book_notes and address_notes.

解决方案

This question introduces unneccessary complexity into the application. Just because the notes have the same structure does not mean that they are the same entity. When modelling the database in 3NF, they are not the same entity because a note can not be moved from a Book to an Address. In your description there is a definitive parent-child relation between book and book_note, etc so model it as such.

More tables is not a problem for the database but unneccessary code complexity is, as this question demonstrates. It is just being clever for clevers sake. This is the trouble with ORMs, people stop doing full normalization and don't model the database correctly.

这篇关于与教义建立一对多的多态关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆