{
    "href": "/post/2004/11/18/db-table-pre-defined-per-query-fetchmodes/",
    "relId": "2004/11/18/db-table-pre-defined-per-query-fetchmodes",
    "title": "DB_Table: Pre-Defined Per-Query Fetchmodes",
    "author": "pmjones",
    "markup": "html",
    "tags": [
        {
            "href": "/tag/php/",
            "relId": "php",
            "title": "PHP",
            "author": null,
            "created": null,
            "updated": [],
            "markup": "markdown"
        }
    ],
    "created": "2004-11-19 04:10:27 UTC",
    "updated": [
        "2004-11-19 04:10:27 UTC"
    ],
    "html": "<p>Ian Eure just provided a patch to <a href=\"http://pear.php.net/DB_Table\">DB_Table</a> 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:</p>\n<pre><code>\n// predefined query for a list of all rows\n$this-&gt;sql['list'] = array(\n    'select'    =&gt; '*',\n    'from'      =&gt; $this-&gt;table,\n    'order'     =&gt; 'some_column DESC',\n    'get'       =&gt; 'all',\n    'fetchmode' =&gt; DB_FETCHMODE_ASSOC\n);\n\n// predefined query for a single row\n$this-&gt;sql['item'] = array(\n    'select'    =&gt; '*',\n    'from'      =&gt; $this-&gt;table,\n    'get'       =&gt; 'row',\n    'fetchmode' =&gt; DB_FETCHMODE_OBJECT,\n    'fetchmode_object_class' =&gt; 'myDataObjectClass'\n);\n</code></pre>\n<p>With that, you can call <code>$dbTable-&gt;select('list')</code> and get an array of rows, or call <code>$dbTable-&gt;select('item', \"id = '9'\")</code> and get an object of type <code>myDataObjectClass</code>.</p>\n<p>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.</p>\n<p><a href=\"http://pear.php.net/DB_Table\">DB_Table</a> 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).</p>\n"
}
