Jump to Navigation

qobject and mysql

After years of programming web applications, I often found myself debugging MySQL queries. Usually, they don’t even show that fast. Sometimes I spend hours trying to find the error of my php script, changing code parts, I once even redone the whole script and wondered why it still doesn’t work until I found the error in the SQL query. Well, people learn from the faults the make, but I didn’t, at least until some time ago. You maybe read an earlier blog entry of mine about QCMS, an object-oriented php5 content management system I was making. I have used a lot of other CMS, in fact, I am still using Mambo for my free hosting service Kawaii Page. Something was always bugging me about programming modules for other CMS, so I figured it isn’t php but MySQL! Creating tables, queries and all those properties… I wanted to improve this behavior in my CMS. Anyway, I ported the key element QObject from php4 to php5, to test it on a production system (China-Restaurants in Berlin).

So what is so special about it? As I wrote before, storing and loading objects into a MySQL database can be a pain in the ass. Especially when you change your object to fit your new needs. Tables need to be changed, queries… Well, from now on, QObject is doing all this for me now! It takes care of all basic needs in MySQL. Creating Tables, setting keys, uniques, indexes, data types, it can even change the table when you add properties or change the data type on the run and of course loading and storing your php object into you database. No more worrying about MySQL anymore. Some very complex queries can’t be done through QObject, but if you are using those, you could also consider using a different language to code your CMS.

Let’s get a little more detailed. Here is a list of the features:

  • Creating Tables (table names are the names of the object class), one table per object class
  • Setting Uniques for all object properties that start with u_
  • Setting Indexes for all object properties that start with i_
  • Ignoring properties that start with _ (good for temporary properties which are not supposed to be stored)
  • Unique ID for each object (O_ID)
  • Loading an O_ID will automatically result in casting the 1object into the correct subclass.
  • Support for TINYINT, SMALLINT, MEDIUMINT, INT, BIGINT, VARCHAR, TEXT, LONGTEXT data types for properties. Dynamic Table reconfiguration on data type mismatch (storing string in an int field will automatically make the field accept strings)
  • Dynamic addition of properties. QObject detects differences between table properties and object properties and will change table to fit the object.
  • HTML Object property editor. Making backends easier to code.

And now even more detailed, the list of methods:

  • load(int O_ID)
    Lets you load the object with given O_ID. Return true when successful.
  • numOf([string condition])
    Return the number of objects of the same class. If condition is specified, it will be included into the SQL query after there “WHERE” clause.
  • loadWhere(mixed properties, mixed values[,[string custom] [,[string orderlimit] [,boolean returnresults]]])
    Loads the object with given properties=values. If there is only one property to be checked, it can be given as string, else, an array is expected. Custom field lets you specify special “WHERE” clauses like “property>value”. Orderlimit field lets you specify “ORDER BY” and “LIMIT” clause properties like “ORDER BY property ASC LIMIT 10,20″. Finally, returnresults specifies if the function should return an array of objects with matches. The function will set the object to the first match if there is more than one.
  • loadAll()
    Returns all objects of the same class as an array.
  • store()
    Stores object in database.
  • remove()
    Deletes object in database.
  • storeFromPostData()
    For use with the HTML Object Editor. Parses the form created by editObjectForm().
  • editObjectForm()
    The HTML Object Editor. Returns HTML code for a form for editing the object. For Saving the object back from the form data, use storeFromPostData().

There are some more private functions, which I won’t specify any further. Here is a quick example of how to use QObject:

//foobar object using qobject
class foobar extends qobject {
var $foo = 0;
var $bar = “foobar”;

//connect to db

//let’s generate some foobars and store them to database
for($i=0;$i<100;$i++) {
$foobar = new foobar();
$foobar->foo = $i;
$foobar->bar.= $i;

//lets load and show them all
$foobars = $foobar->loadAll();
foreach($foobars as $foobar)
echo $foobar->foo.” -> “$foobar->bar;

Well, as you can see, I didn’t need to use any MySQL at all to perform all these database operations.

On my production system test, QObjects works quite well. I haven’t had any problems with it.

Anyway, here is it for you to use if you want give it a try:
Download QObject

Comments are closed.