DB_Table: Pre-Defined Per-Query Fetchmodes

Ian Eure just provided a patch to DB_Table that is genius in its simplicity. While I have not tested it, the patch looks like it will cause no trouble. In short, in your predefined SQL queries, you can specify that the query should return in DB_FETCHMODE_ASSOC, DB_FETCHMODE_ORDERED, or any other fetchmode. For example:


// predefined query for a list of all rows
$this->sql['list'] = array(
    'select'    => '*',
    'from'      => $this->table,
    'order'     => 'some_column DESC',
    'get'       => 'all',
    'fetchmode' => DB_FETCHMODE_ASSOC
);

// predefined query for a single row
$this->sql['item'] = array(
    'select'    => '*',
    'from'      => $this->table,
    'get'       => 'row',
    'fetchmode' => DB_FETCHMODE_OBJECT,
    'fetchmode_object_class' => 'myDataObjectClass'
);

With that, you can call $dbTable->select('list') and get an array of rows, or call $dbTable->select('item', "id = '9'") and get an object of type myDataObjectClass.

This is only in CVS right now, as I’m trying to add various Oracle restrictions to DB_Table, but it was just too neat to not add in right away. Thanks, Ian Eure.

DB_Table is a PHP class to automatically create RDBMS tables and XHTML forms. It includes a “poor man’s” data type abstraction wherein the database is forced to store date and time data in ISO formats, which means you do not need to do date/time magic on your queries before executing them (and you don’t need to do magic on the return results, either).

Are you stuck with a legacy PHP application? You should buy my book because it gives you a step-by-step guide to improving your codebase, all while keeping it running the whole time.
Share This!Share on Google+Share on FacebookTweet about this on TwitterShare on RedditShare on LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *