September 17, 2006

Catching up (8, 9, 10)

I’ve been a little busy with moving, so that is my excuse for being AWOL. Let’s continue.

I learned that Ikea dressers require far more effort to build than beds. Probably twice as much time and effort.

I learned that the California Department of Corporations has wacky accounting that seems to work against their best interest. I just received a full refund for a processing fee they kept requesting last year. Strange.

I learned that it is unexpectedly rare to find web developers who have tried creating their own object-oriented database abstraction layer. I found it is even more rare to find developers who took this abstraction layer and made the (in my opinion) relatively obvious step toward creating a generalized abstraction layer that removes the need to write SQL 90% of the time. For those of you who haven’t thought about this before, creating such a layer, the pride and joy of the rails movement, is relatively simple. While there are many schools of thought on how to accomplish this, I think a simple place to start is to setup something like this:

// Notice my example assumes any table you want.
$object = new DBLayer(’tablename’);
// Runs an equivalent of SELECT * FROM tablename WHERE
// primary_key_field = 30;

$object->load(30);
// Overload PHP5’s __set() method (see documentation)
// store this in an internal array so that table fields like
// ‘tablename’ don’t accidentally erase object settings.
// Thus, “$this->mData['username'] = $value;”
// Just see the documentation of __set(). Trust me.
$object->username = ‘new username’;
// runs an equivalent of UPDATE tablename SET username=’new
// username’ WHERE primary_key_field = 30;

$object->save();

There are many ways to figure out the primary key. One idea is to standardize the primary key name so that “tablename” always has a primary key of “tablename_id”. Another idea is to dynamically determine it by running a “DESC tablename” and caching the results. Think it over. It’s an interesting, but highly insightful challenge. My example may be a little advanced, but this is the starting point of those shiny “rails frameworks” you hear about.

Filed under: Business, Life, PHP, Programming — Michi @ 12:30 am

Share this

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Reddit
  • StumbleUpon
  • del.icio.us
  • description
  • Technorati
  • Slashdot
  • co.mments
  • NewsVine

Related

While Internet users have been rising rapidly over the past few years, it seems blogs have not kept up. Sure, there's been tons of new blogs, but it seems for every new blog that started up, another one died. Thus,...
Google is finally saying "I do" to online productivity suites. Google has been beta testing a new service called Google Apps for Your Domain - a service that lets companies use Google email accounts with their own domains. Additionally, it...

1 Comment »

TrackBack URI | Blog RSS | Comment RSS

  1. [...] The first is to write a database abstraction layer. What? A database abstraction “layer” is a fancy word for a class that manages your database connection and data manipulation. I gave a brief example of how to write one of these a while back. Another is to create a function (or method, if you’re talking about classes) that does INSERT and UPDATE querying for you. Such a function’s prototype would look like this: function perform($tableName, $data, $whereClause) [...]

    Pingback by MichiKono.com » I Hate Magic Quotes — November 15, 2006 @ 1:28 am

What do you think?