php – Storing array in database without serializing


I'm building a social networking website where the user can belong to groups and comment on posts.

I'm trying to cleanly store group members and comments in the database, preferably without serializing the arrays, but I'm not finding a way to do it.

I had thought about keeping a table with this data inside the groups and posts lines, but I found that this is not possible.

Is there any clean way to save this data or do I have to serialize the user and comment vectors?


Solution 1

Gcanhete, the ideal is to review your database modeling, there must be a clean way to store the data in the database, preferably obeying the first 3 formal rules.

I understand that the way you want to do it now you are wanting to store a bank line for each group and comment, however this is not tenable, for example, what size field will you use to accommodate this data, what will you do when there are more comments than you expect?

The ideal is to store it in such a way that each comment has its own record, with all the information pertaining to them.

If the problem is performance then it is the case to adjust in other points (indices, cache, etc).

If you still want to store the array inside the database, you still have options:

  • Fully store (pass array to string and store)
    • In this case you still have the benefits of being able to search and change, being directly in SQL as in other languages ​​(for APIs for example).
  • Store serial (go through serialization)
    • You lose some editing and readability as any mistake can invalidate the information.
  • Encode with Base64 or other reversible algorithms.

Solution 2

Another option if you want to store a data structure that is not rigid (like a relational database) is to use NoSQL databases like Redis and MongoDB, where you can pass your entire arrays in for persistence.

In this second case you can store them and retrieve them practically ready for use.

Scroll to Top