PHP: bad design serializes objects and inserts them into the database later?

I am planning and exploring my transition 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 put together a collection of embedded documents. They never need to be looked for. Is it possible to just serialize PHP objects, insert them into the database and then not initialize them back to PHP objects when I want to use them? An alternative is to use Doctrine as my ORM.

My programming intuition tells me that this is poor design and limits, but I feel that serialization and non-esterization will be very fast and eliminate the need for ORM.

What is your opinion? Good design or bad design?

+4
source share
4 answers

In many cases, this will be considered a poor design, but it can work if all of the following conditions apply:

  • You do not need to search by them.
  • You can accept the (potentially) limited ability to request them
  • You do not need relational integrity or other restrictions applied by the RDBMS.
  • You know that you will never have to read them in another language.
  • Are you sure that you know how to deserialize, version and properly migrate them when updating the class definition.
  • Are you sure that the PHP serialization format will be stable in all versions (or are you ready to write a migration code or is it a short-term project, and you don't care)
  • Are you ready to accept a slight performance limitation ( SELECT + deserialize() will be slower than just SELECT )
+9
source

Why use a database if you cannot query it?

+3
source

It depends entirely on what you intend to do.

If it is always the same object that each request deals with, or if there is no relationship between each request, this may be normal.

But I have many shortcomings:

  • You might want to do something more advanced for objects later.
  • Serialized objects are unreliable (not entirely compatible with ACID).
  • There is nothing else that a serialized php object can read; instead, you can use something else.
+1
source

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

I find it useful to use it for serialization, but I also need to make sure that it can never be edited as a string without first initializing it!

0
source

Source: https://habr.com/ru/post/1338362/


All Articles