As I said before, the last example I’m going to show you illustrates the entire process for storing serialized objects in a MySQL table. To begin with, I’ll define a simple “objects” database table, composed of “id” and “users” fields, which will house the IDs of stored objects and their serialized structure. Here is the SQL statement that creates this table: CREATE TABLE objects ( As you can see, the table for storing serialized objects is fairly simple, thus now I’m going to build a basic “User” class, which will store some data about fictional people, such as Name, Postal Address and Email. The signature of the class looks like this: // define 'User' class
That’s it. Now I defined the corresponding “User” class, which will serve to spawn as many objects as required, and after being serialized, these objects will be stored in the “objects” sample table. There’s still one more thing that I wish to do before proceeding to store objects in a MySQL table: I’ll create an object serializing class, in order to handle the complete serialize/unserialize process by using its interface. The source code of this class, which I called “ObjectSerializer”, is shown below: class ObjectSerializer{ In this case, the above class was defined as a simple wrapper for the pair of “serialize()/unserialize()” PHP functions, so it does not require much discussion about how it works. Given that, the next step consists of populating the “objects” table with some users, so first I’ll proceed to create them, like this: // instantiate some 'User' objects All right, now that some fictional users have been created, it’s time to serialize them and store them in the “objects” database table. To do this, I’ll use a couple of MySQL wrapper classes that will be listed at the end of the example. Here is how the entire set of “Users” objects are stored in the corresponding table: // connect to MySQL As you can see, the above script first connects to MySQL, then instantiates a new “ObjectSerializer” object, and finally inserts all the serialized “User” objects into the sample “objects” database table. Definitely, you’ll agree with me that this step is simple and comprehensive. Once the respective users have been stored, I’m going to reverse the process and fetch the objects from the respective table, so I’m able to use their methods to display the information attached to them. Take a look at the piece of code below that precisely demonstrates how the objects are restored: // restore objects from database table In this case, objects are first fetched from the database table by using a regular SELECT statement, and then unserialized by the “getUnserializedObj()” method. Finally, the data associated with each user is displayed by calling each of the corresponding getters. Of course, below I listed the output generated by the previous example: ID: 1 Name: John Doe Postal Address: 5456 Protocol Avenue MA Email: john@domain.com Even when you’re usually storing your binary objects in BLOB table fields, the approach that I just showed you before can be considered a useful alternative for saving objects as plain strings.
blog comments powered by Disqus |
|
|
|
|
|
|
|