PHP OOP:为域实体提供“身份”

发布于 2024-08-29 23:36:52 字数 893 浏览 1 评论 0原文

这里有一个抽象的问题。我正在尝试域模型模式,并禁止我与依赖项发生其他冲突 - 我需要一些关于生成用于身份映射的身份的建议。

在我见过的大多数数据映射器模式示例中(包括本书中概述的示例:http:// /apress.com/book/view/9781590599099) - 用户似乎使用设置器手动设置给定域对象的身份:

$UserMapper = new UserMapper;

//returns a fully formed user object from record sets
$User = $UserMapper->find(1);

//returns an empty object with appropriate properties for completion
$UserBlank = $UserMapper->get();
$UserBlank->setId();
$UserBlank->setOtherProperties();

现在,我不知道我是否读错了示例 - 但是在第一个 $User 对象中,从数据存储中检索 $id 属性(我假设 $id 代表行 ID)。然而,在后一种情况下,如果对象尚未从数据存储中获取 $id,如何设置该对象的 $id 呢? 问题是为对象生成有效的“身份”,以便可以通过身份映射来维护它 - 因此生成任意整数并不能解决问题。

我目前的想法是指定不同的身份字段(即电子邮件)并要求它们在生成空白域对象时存在。或者,要求所有对象完全形成,并使用所有属性作为它们的标识……效率很低。

(或者,转储域模型概念并返回 DBAL/DAO/事务脚本...与我见过的 ORM 实现相比,这似乎越来越优雅...)

Bit of an abstract problem here. I'm experimenting with the Domain Model pattern, and barring my other tussles with dependencies - I need some advice on generating Identity for use in an Identity Map.

In most examples for the Data Mapper pattern I've seen (including the one outlined in this book: http://apress.com/book/view/9781590599099) - the user appears to manually set the identity for a given Domain Object using a setter:

$UserMapper = new UserMapper;

//returns a fully formed user object from record sets
$User = $UserMapper->find(1);

//returns an empty object with appropriate properties for completion
$UserBlank = $UserMapper->get();
$UserBlank->setId();
$UserBlank->setOtherProperties();

Now, I don't know if I'm reading the examples wrong - but in the first $User object, the $id property is retrieved from the data store (I'm assuming $id represents a row id). In the latter case, however, how can you set the $id for an object if it has not yet acquired one from the data store?
The problem is generating a valid "identity" for the object so that it can be maintained via an Identity Map - so generating an arbitrary integer doesn't solve it.

My current thinking is to nominate different fields for identity (i.e. email) and demanding their presence in generating blank Domain Objects. Alternatively, demanding all objects be fully formed, and using all properties as their identity...hardly efficient.

(Or alternatively, dump the Domain Model concept and return to DBAL/DAO/Transaction Scripts...which is seeming increasingly elegant compared to the ORM implementations I've seen...)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

允世 2024-09-05 23:36:52

如果您要控制 ID、想要覆盖数据存储 ID、或者想要更新/删除数据而无需先检索数据(即已经从 POST 获取 ID),则可以使用 setId 函数。
另一种替代方法是调用 setId() 通过“查询”(插入记录)数据存储中下一个可用 ID 来保留 ID。

在您实际需要使用 ID 来引用某些内容之前,ID 设置的内容并不真正相关。调用不带参数的 setId 除了将对象标记为新数据之外不会执行任何操作。一旦您真正尝试“获取”ID,就会生成 ID。对惰性 ID 生成进行排序。

You would use the setId function if you are controlling the IDs, if you want to override the data store ID, or if you want to update/delete the data without having to retrieve it first (i.e. already have the ID from a POST).
Another alternative would be calling setId() to reserve an ID by "querying" (insert a record) the data store for the next available ID.

It's not really relevant what the ID is set to until you actually need to use it to reference something. Calling setId with no parameter would do nothing except flag the object as new data. Once you actually try to "get" the ID is when one would be generated. Sort lazy ID generation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文