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.

要注意(没有双关语;),您可能会注意到,基于与之相关的对象,note从来不需要唯一的属性。

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

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

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.

至少我要解决的问题是避免有三个不同的表:image_notes、book_notes和address_notes。

5 个解决方案

#1


4

Personally I wouldn't use a superclass here. Think there's more of a case for an interface that would be implemented by Book, Image and Address:

我个人不会在这里使用超类。认为有更多的情况下,一个接口将实现图书,图像和地址:

interface iNotable
{
    public function getNotes();
}

Here's Book as an example:

这里有本书作为一个例子:

class Book implements iNotable {
    /**
     * @OneToMany(targetEntity="Note", mappedBy="book")
     **/
    protected $notes;

    // ...        

    public function getNotes()
    {
        return $this->notes;
    }
}

Note then needs @ManyToOne relationships going the other way, only one of which will apply for each entity instance:

注意,需要使用@ManyToOne关系,只有一个关系适用于每个实体实例:

class Note {
    /**
     * @ManyToOne(targetEntity="Book", inversedBy="notes")
     * @JoinColumn
     **/
    protected $book;

    /**
     * @ManyToOne(targetEntity="Image", inversedBy="notes")
     * @JoinColumn
     **/
    protected $image;

    /**
     * @ManyToOne(targetEntity="Address", inversedBy="notes")
     * @JoinColumn
     **/
    protected $address;

    // ...
}

If you don't like the multiple references to each notable class here you could use inheritance and have something like an AbstractNotable superclass using single table inheritance. Although this might seem tidier now, the reason I wouldn't recommend it is as your data model grows you may need to introduce similar patterns that could eventually make the inheritance tree become unmanageable.

如果您不喜欢这里对每个值得注意的类的多个引用,那么您可以使用继承并使用类似抽象值得注意的超类的单个表继承。虽然现在看起来更整洁了,但我不推荐它的原因是,随着数据模型的增长,您可能需要引入类似的模式,这些模式最终可能会使继承树变得不可管理。

EDIT: It would also be useful to have Note validator to ensure data integrity by checking that exactly one of $book, $image or $address is set for each instance. Something like this:

编辑:通过检查是否为每个实例设置了$book、$image或$address中的一个,使用Note validator来确保数据完整性也很有用。是这样的:

/**
 * @PrePersist @PreUpdate
 */
public function validate()
{
    $refCount = is_null($book) ? 0 : 1;
    $refCount += is_null($image) ? 0 : 1;
    $refCount += is_null($address) ? 0 : 1;

    if ($refCount != 1) {
        throw new ValidateException("Note references are not set correctly");
    }
}

更多相关文章

  1. javascript判断数组和对象中是否存在某元素
  2. 视差滚动与图像仅通过徽标透明度
  3. 如何将对象作为参数传播给函数?
  4. RangeError:在Node.js中调试/记录/检查对象时超过了最大调用堆栈
  5. 是否有一个简单的库将JSON对象渲染为树?
  6. 在JavaScript中进行文件处理,第四部分:对象URLs
  7. 我在显示随机选择的对象时遇到问题
  8. 如何在JavaScript / jQuery中获取对象的属性?
  9. 【JavaScript】JavaScript的对象-对象专门语句

随机推荐

  1. Android动画
  2. 布局(1、线性布局)
  3. ListPreference
  4. 不停地切换两张图片ViewFlipper
  5. 2.6.2 Notification的功能与用法
  6. Android中的时间日期选择器
  7. Android(安卓)添加系统服务的方法
  8. 2.5.2 使用alertdialog 创建列表对话框
  9. ViewFlipper+GestureDetector实现不循环
  10. Android RelativeLayout相对布局属性简析