PHP:序列化对象并将其保存在数据库中供以后使用是不是不好的设计?

发布于 2024-10-15 23:50:58 字数 278 浏览 10 评论 0原文

我现在正在计划和研究从 MySQL 到 MongoDB 的切换,我有一个有趣的想法......我有一堆分层对象需要存储在数据库中。我当前的方法是在集合中包含一堆嵌入文档。他们永远不需要被寻找。仅序列化 PHP 对象,将它们粘贴到数据库中,然后在我想要使用它们时将它们反序列化回 PHP 对象是否有意义?另一种方法是使用 Doctrine 作为我的 ORM。

我的编程直觉告诉我,这是一个糟糕的设计并且有限制,但我觉得序列化和反序列化会非常快,并且消除了对 ORM 的需要。

你有什么意见?好的设计还是坏的设计?

I am planning and researching my switch from MySQL to MongoDB right now and I just had an interesting thought... I have a bunch of hierarchical objects that I need to store in the database. My current method is to have a bunch of embedded documents in a collection. They will never need to be searched for. Would it possibly make sense just to serialize the PHP objects, stick them in the DB, and then unserialize them back into PHP objects when I want to use them? The alternative is using Doctrine as my ORM.

My programming intuition tells me that this is bad design and is limiting, but I feel like serializing and unserializing would be very fast and eliminate the need for an ORM.

What's your opinion? Good design or bad design?

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

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

发布评论

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

评论(4

灰色世界里的红玫瑰 2024-10-22 23:50:58

在许多情况下,这会被认为是糟糕的设计,但如果满足以下所有条件,它可能会起作用:

  1. 您不需要搜索它们
  2. 您可以接受(可能)有限的查询它们的能力
  3. 您不需要关系完整性或RDBMS 强制执行的其他约束
  4. 您知道您永远不需要以不同的语言阅读它们
  5. 您确信在更新类定义时您将知道如何正确地反序列化、版本化和迁移它们
  6. 您确信PHP 序列化格式将在各个版本中保持稳定(或者您愿意编写迁移代码,或者这是一个短期项目,您不在乎)
  7. 您愿意接受较小的性能损失(SELECT + deserialize() 会比 SELECT 慢)

In many cases this would be considered bad design, but it could work if all of the following apply:

  1. You don't need to search on them
  2. You can accept (potentially) limited ability to query on them
  3. You don't need relational integrity or other constraints enforced by the RDBMS
  4. You know you'll never need to read them in a different language
  5. You're confident that you'll know how to deserialize, version, and migrate them properly when you update your class definition
  6. You're confident that the PHP serialization format will be stable across releases (or you are willing to write migration code, or it's a short-term project and you don't care)
  7. You're willing to accept a minor performance penalty (SELECT + deserialize() will be slower than just SELECT)
笑饮青盏花 2024-10-22 23:50:58

如果无法查询,为什么还要使用数据库?

Why use a database if you can't query it ?

不语却知心 2024-10-22 23:50:58

这完全取决于您打算做什么。

如果每个请求处理的始终是同一个对象,或者每个请求之间没有关系,则可能没问题。

但对我来说,有很多缺点:

  • 您可能想稍后对对象做一些更高级的事情
  • 序列化对象有点不可靠(不完全符合 ACID)
  • 没有其他东西可以读取序列化的 php 对象,您可能想要使用而是用别的东西。

It kind of depends entirely on what you intend to do.

If it's always the same object each request deals with or there are no relationships between each request, it might be ok.

But to me there are a lot of downsides:

  • You might want to do something more advanced to the objects later
  • Serialized objects are kind of unreliable (not exactly ACID compliant)
  • There's nothing else that can read a serialized php object, you might want to use something else instead.
错々过的事 2024-10-22 23:50:58

当您必须缓存某些内容(例如 RSS 提要)时,序列化对象非常有用。

我发现序列化它很有用,但我也会确保在不先反序列化它的情况下永远不能将其作为字符串进行编辑!

Serializing objects is VERY useful when you have to cache things, such as RSS feeds.

I find it good use to serialize it, but I would also make sure that that can never be editing as a string without unserializing it first!

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