I’m looking for advice on something I’m building: I have a PHP app that creates, updates and deletes records, but it’s tightly integrated in a legacy non-sql based database, and it gets to be really really slow when you start doing lots of DB calls.
I want to make this more of a background experience for the user, so that when user creates or edits something, all the variables, arrays and objects would need to be written to a MySQL database and then a background script would kick off to read those records and process the request to the legacy database.
So I would need one table that keeps track of the task and then another table that would track all of the variables, objects, arrays and their values.
Here’s what I was thinking the DB structure of the 2nd table would have to be:
- A column to store the task_id
- A column to store if the var is an array or an object, or NULL if it’s a simple var.
- A column to store the name of the array/object var.
- A column for if it’s an object, then to store the type of object it is.
- A column to store the array/object group ID. (For keeping track of what vars belongs to the object/array)
- A column to store the name of a simple var, method or name in the object/array
- A column to store the value of the var
Here would be a few examples:
1 | NULL | NULL | NULL | NULL | 'foo' | 'bar'
1 | 'array' | 'foo_array' | NULL | 1 | 'foo' | 'bar'
1 | 'array' | 'foo_array' | NULL | 1 | 'foo2' | 'bar2'
1 | 'object' | 'foo_obj' | foobar_object | 2 | 'foo_method' | 'bar'
1 | 'object' | 'foo_obj' | foobar_object | 2 | 'bar_method' | 'foo'
Does this seem like an overly complicated approach? Am I crazy and over thinking this? Can someone think of a better way I should approach this?
Thanks.
Use serialize()
It will generate a specially formatted string(you can store in your database). The string is special in that unserialize() can translate it back into its original php value. It maintains a variables type, value, and structure. Custom objects, and multidimensional arrays are no problem.
You still need part of your db table, this just does a lot of the work for you, and does it very well.