PHP Datamapper-为什么将它们用于非集合对象? [英] PHP datamapper - why use them for non-collection objects?

查看:82
本文介绍了PHP Datamapper-为什么将它们用于非集合对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许这是一个琐碎答案的问题,但是尽管如此,它让我发疯了几天,所以我想听听答案。我最近正在查找有关为自己的项目(而不是使用ORM)构建自定义数据映射器的大量信息,并在stackoverflow或其他网站上读取了多个线程。

Perhaps this is a question with a trivial answer but nevertheless it is driving me nuts for a couple of days so i would like to hear an answer. I'm recently looking up a lot of information related to building a custom datamapper for my own project (and not using an ORM) and read several thread on stackoverflow or other websites.

拥有 AuthorCollection 对象对我来说似乎很有说服力,这些对象基本上只是 Author 实例或 BookCollection 对象,其中包含多个 Book 实例。但是,为什么要为单个 Author 对象需要一个映射器?我能想到的所有提取条件(要求使用指定的 BookID AuthorID 的对象除外)将返回多个 Book Author 实例,因此 BookCollection AuthorCollection 实例。那么,为什么要为单个对象设置一个映射器呢?如果一个用于适当集合的映射器更笼统,而您不必确保条件只会返回一个结果呢?

It seems very convincing to me to have AuthorCollection objects, which are basically only a container of Author instances or BookCollection objects, which hold multiple Book instances. But why would one need a mapper for the single Author object? All fetch criterias i can think of (except the one asking for the object with a specified BookID or AuthorID) will return multiple Book or Author instances hence BookCollection or AuthorCollection instances. So why bother with a mapper for the single objects, if the one for the appropriate collection is more general and you don't have to be sure that your criteria will only return one result?

预先感谢您的帮助。

推荐答案

简短答案


您不必为 Author AuthorCollection 创建两个映射器。如果您的程序不需要 AuthorMapper AuthorCollectionMapper 即可顺利运行并获得干净的源代码,请按照

Short answer

You don't need to bother creating two mappers for Author and AuthorCollection. If your program doesn't need an AuthorMapper and an AuthorCollectionMapper in order to work smoothly and have a clean source, by all means, do what you're most comfortable with.


注意:选择此路线意味着您应该格外小心违反违反SRP 的人。

Note: Choosing this route means you should be extra careful looking out for SRP violations.




长答案


这一切都取决于您要执行的操作。为了方便起见,我们将 AuthorMapper 称为项目数据映射器,将 AuthorCollectionMapper 称为集合数据映射器。


Long(er) answer

It all depends on what you're trying to do. For the sake of this post, let's call AuthorMapper an item data mapper and AuthorCollectionMapper a collection data mapper.

通常,项目映射器不会像其集合映射器那样复杂。项映射器通常仅通过主键获取,因此限制了结果,使映射器干净且不受其他特定于集合的事物的干扰。

Typically, item mappers won't be as sophisticated as their collection mappers. Item mappers will normally only fetch by a primary key and therefore limit the results, making the mapper clean and uncluttered by additional collection-specific things.

一个主要部分这些特定于集合的事物中的我提出的是条件 1 以及它们如何在查询中实现。通常,在集合映射器中,您可能会比项目数据映射器中通常具有更高级,更冗长且乏味的查询。尽管完全有可能将您的平均商品数据映射器查询( SELECT ... WHERE id =:id )与一个复杂的集合映射器查询 使用有臭味的条件 2 ,它变得更加复杂,并且当它需要的只是一个简单的通用查询时,仍然困扰着数据库执行冗长的查询。

One main part of these "collection-specific things" I bring up is conditions1 and how they're implemented into queries. Often within collection mappers you'll probably have more advanced, longer, and tedious queries than what would normally be inside an item data mapper. Though entirely possible to combine your average item data mapper query (SELECT ... WHERE id = :id) with a complicated collection mapper query without using a smelly condition2, it gets more complicated and still bothers the database to execute a lengthy query when all it needed was a simple, generic one.

此外,尽管您指出,对于项目映射器,我们实际上仅是通过主键来获取的,但事实证明,使用项目映射器进行其他操作通常会更加简单。项目映射器的 save() remove()方法可以(通过正确的实现)胜过尝试使用集合映射器保存/删除项目。并且,随着这一点,很明显有时在整个使用集合映射器的 save() remove()方法,则集合映射器可能要使用项目映射器方法。

Additionally, though you pointed out that with an item mapper we really only fetch by a primary key, it usually turns out to be radically simpler using an item mapper for other things. An item mapper's save() and remove() methods can handle (with the right implementation) the job better than attempting to use a collection mapper to save/remove items. And, along with this point, it also becomes apparent that at times throughout using a collection mappers' save() and remove() method, a collection mapper may want to utilize item mapper methods.

为回答以下问题,您可能有很多次要设置条件来删除以下项的集合数据库中的行。例如,您可能有一个垃圾邮件标志,该标志设置后会隐藏帖子,但会在30天后自毁。在这种情况下,您很可能会对垃圾邮件标志设置一个条件,对时间范围设置一个条件。另一种可能是删除答案后三十天删除答案下的所有注释。我提到30天是因为明智的做法是至少保留一小段时间,以防有人要发表评论,或者发现带有垃圾邮件标志的行实际上不是垃圾邮件。

In response to your question below, there may be numerous times you may want to set conditions in deleting a collection of rows from the database. For example, you may have a spam flag that, when set, hides the post but self-destructs in thirty days. I'm that case you'd most likely have a condition for the spam flag and one for the time range. Another might be deleting all the comments under an answer thirty days after an answer was deleted. I mention thirty days because it's wise to at least keep this data for a little while in case someone should want their comment or it turns out the row with a spam flag isn't actually spam.


1。这里的条件表示在集合实例上设置的属性,集合映射器的查询知道该属性如何处理。如果还没有,请查看 @tereško's 回答在这里

<子> 2。此条件有所不同,并指的是如果……则邪恶 人们说到。如果您不了解他们的恶意,建议您观看一些干净代码讨论。 专门为此,但都很棒。

1. Condition here means a property set on the collection instance which the collection mapper's query knows how to handle. If you haven't already, check out @tereško's answer here.
2. This condition is different and refers to the "evil if" people speak of. If you don't understand their nefariousness, I'd suggest watching some Clean Code Talks. This one specifically, but all are great.

这篇关于PHP Datamapper-为什么将它们用于非集合对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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