Flatterizor is a PHP library developed in order to issue a very specific problem.
Sometimes you have a complex object and you need to store it into your database. Most cases the serialize function does work. Unfortunately sometimes it does not ( not because serialize is broken/bugged ).
The Library is useful only if the followings are true:
- You want to store your object inside a database
- Your object is not “Flat” ย ( meaning you’ve at least a value in your object that is an array or an object itself )
- You want to use your Database SQL to Search & filter objects with a particular value.
The last sentence is actually the most appealing. Lets take, for example, the following objects:
Mat and Bob are 2 users. We’d like to persist them in a database butย You can’t design a Database Table that scales well. Why?
- Cause maybe you’ll also add a “surname” key sometimes in the future
- Cause you don’t know if you’ll add other permissions in the future ( Sub-Set of the previous sentence )
So, what stops us to use serialize() ? Well, take a look to how $mat looks when “serialized”
Good Luck creating an SQL Query that finds all the users that have the permission to write. ๐
Using Flatterizorย you’ll have a completely different output. First, lets see how our code would change if we’re going to use the library.
The output of the flatterize method is an object with 2 keys:
- objectDefinitions: ย Used to rebuild the object it contains a plain array of strings with the name of the classes and their paths
- values: ย An array of arrays. Each array will contain 2 keys
- path: ย It’s the path of the value
- val
Let’s take a look to the var_dump output.
The end goal was to persist the object inside a database so that we could easily search through an SQL Query. For the example I designed the following database table.
In our example we’ll need to perform a loop on $matGoodLooking->values and insert the values inside the table where
- ‘path‘ will be mapped in the ‘name‘ column
- ‘val‘ ย will be mapped in the ‘val‘ column
userID | name | val |
---|---|---|
1 | /name | Mat |
1 | /permissions/[can_write] | false |
1 | /permissions/[can_read] | true |
2 | /name | Bob |
2 | /permissions/[can_write] | true |
2 | /permissions/[can_read] | true |
With a very simple SQL Statement we’ll be able to find all the ย users that have the ability to write.
Note: It’s not hard to understand that if you’ve a lot of users, the table is going to fill your disk space very quickly. There’re a lot of solutions to this so I won’t cover them here.
Sooner or later you’ll want to retrieve your users in the original form. Doing that is very easy… just use the static method calledย ObjectFlatterizor::rebuild . It accepts 1 parameter which is the exact output of the flatterize() method… you’ll be required to rebuild it correctly.
Using with WordPress
The idea came working with WordPress. I had a Box inside the “Post Edit” screen, this box contained ~30 input fields. I then created a way to save those fields in an object. Soon I realized I needed to search posts using this object values… since WordPress provides a way to search posts using the post_meta values, ย I built the library to serialize/deserialize my objects in the post_meta table.
The whole thing works very well ๐
Authors
- Andrea Baccegaย [email protected]ย –ย Author/Ideator of the library
- Emanuele ‘Tex’ Tessoreย [email protected]ย –ย Contributor of the library
More infos
I strongly suggest to:
- take a lookย @ github
- take a lookย @ the demo folder