Storing array in database without serializing

Asked

Viewed 929 times

3

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

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

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

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

  • 1

    Not possible. What problem with serializing?

  • 1

    Do you think it takes more trouble to serialize than to create a POG? If you turn an array into a string using the types of information you want, it will be IMPOSSIBLE to reverse the process assuming that the comments will ensure the use of any char by the user.

1 answer

1

Solution 1

Gcanhete, the ideal is to review your database modeling, there should be a clean way to store the data in the database, preferably following the first 3 formal standards.

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

The ideal is to store so that each comment has its own record, with all relevant information to them.

If the problem is performance ai is the case of adjusting at 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 it)
    • In this case you still have the benefits of being able to search and change and can be direct in SQL as in other languages (for Apis for example).
  • Serial storage (go through serialization)
    • You lose some of the ability to edit and read, since any error can invalidate the information.
  • Encoding 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 for persistence.

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

  • 1

    1. If you use implode will keep the value but will lose the keys. Serialize or json are the indicated. 2. base64_encode( string $data ), logo does not "encrypt" an array.

  • I will try the Nosql database solution. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.