19 Mar 2013
Contrary to my online handle "Object-Oriented David" I use Object-Oriented Programming very sparingly. I do however try to use the right tool for the job and take on as many of the concepts that I love about frameworks and libraries I admire.
In creating a "query builder" for the NexGen platform I ended up wanting to use syntax that I would associate with jQuery; chained and namespaced with a common bubbling of methods and properties. Thus I designed a very simple class (it's simple, but it took me about 2 hours of experimentation to make!)
Here's the code - with example
In my implementation you can run queries like these:
query->sql('SELECT TABLES')->execute()->debug(); query->select('*')->from('users')->join('avatar')->where('id', 1)->limit(1)->execute()->debug(); query->delete()->from('users')->where('id', 10)->limit(1)->execute()->debug(); query->upsert('users')->data(array( 'id'=>NULL, 'email'=>'firstname.lastname@example.org' ))->return(TRUE)->execute()->debug();
As you can see, each use execute() and debug(), lets explain what happens: