Hi
Receiving the table name for an column is quite useful.
It is not possible for all PDO database drivers to support this
functionally, still at least the SQLite version could support it when
Bug #42589 is resolved. ATTR_FETCH_TABLE_NAMES should be added to SQLite
too.
Currently we have 2(3) ways to get a table name for a column.
PDOStatement -> getColumnMeta
PDO::ATTR_FETCH_TABLE_NAMES (Not in the SQLite patch)
A patch to support new fetch modes ( FETCH_2D) by Hans-Peter Oeri
It would be nice if something like FETCH_2D would be possible, so let's
purpose a fourth way in the spirit of Hans-Peters patch. :o)
PDO::FETCH_ASSOC_TREE
Using the column key to build a hierarchical tree structure by using the
dot as delimiter for the keys.
Together with PDO::ATTR_FETCH_TABLE_NAMES this would result into mostly
the same functionality as FETCH_2D | FETCH_ASSOC as in the FETCH_2D
patch. Joining with different tables (with same column names) without
doing any manually aliasing would end up to be quite easy:
SELECT test1.*, test2.* FROM test1 LEFT JOIN test2 ...
And could then post processed to get the relations right and/or
converting to objects quite easily.
FETCH_CLASS_TREE would be even nicer but there isn't a good way to do
that. (Would need new interfaces to connect keys/table names to specific
classes)
When ATTR_FETCH_TABLE_NAMES isn't support it is still possible to
manually redirect every field by an alias name in the SQL string and
benefit from the automatically converting of result rows into a tree
like structure.
Surely FETCH_ASSOC_TREE could be written at script level, I do think the
"split at dot and traversing" part is better done in the PDO extension
and comparing to FETCH_2D it only really touches PDO fetch without any
need to change the PDO drivers. (But could be optimized later to skip
parts of ATTR_FETCH_TABLE_NAMES in the driver)
So what do you think?
------
Oskar Eisemuth
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php