Req #42664 [Opn->Wfx]: PDO::FETCH_CLASSTYPE is not useful for abstracting code.
| From: | cmb@php.net | Date: | Wed, 29 Sep 2021 16:02:15 +0000 |
| Subject: | Req #42664 [Opn->Wfx]: PDO::FETCH_CLASSTYPE is not useful for abstracting code. | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-236925@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=42664&edit=1
ID: 42664
Updated by: cmb@php.net
Reported by: jdriddle_producer at yahoo dot como
Summary: PDO::FETCH_CLASSTYPE is not useful for abstracting
code.
-Status: Open
+Status: Wont fix
Type: Feature/Change Request
Package: PDO related
Operating System: *
PHP Version: 5.2.4
-Assigned To:
+Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
Given that there was barely any traction on this issue for 14
years, and that it can easily been worked around, I'm closing as
WONTFIX.
If anybody is still interested in this feature, please pursue the
RFC process[1].
[1] <https://wiki.php.net/rfc/howto>
Previous Comments:
------------------------------------------------------------------------
[2011-09-12 21:09:34] restlesslythought at gmail dot com
You actually don't have to list out all the column names. You can just use *
again.
This works perfectly:
$query = "SELECT
classType, $tableName.* from $tableName";
------------------------------------------------------------------------
[2007-09-13 19:29:15] jdriddle_producer at yahoo dot como
After additional thought, it might be better to add this functionality to
PDOStatement->fetchObject() instead. That way there would be no chance of mucking up
PDOStatement-> fetch() with the added parameters.
------------------------------------------------------------------------
[2007-09-13 18:37:49] jdriddle_producer at yahoo dot como
Description:
------------
Currently, the PDO::FETCH_CLASSTYPE option is used with PDO::FETCH_CLASS in $result->fetch() in
order to create an object based upon a classname determined by data in the table. Unfortunately, the
method looks for the FIRST COLUMN in the result. This is counter to common relational database
design.
The only way to currently work around this and get at the desired functionality is to abandon the
common 'SELECT * from $tableName' construct that is so useful in abstraction, and use
custom 'SELECT classType, col2,col3,... from _tableName_' for each query.
I suggest that the PDO::FETCH_CLASSTYPE option be changed to take an optional parameter, $colName,
for the column name to look for the class name in. The current behavior would remain as the default.
This would allow:
$objArray = [];
$query = "SELECT * from $tableName";
$result = $dbh->query($query);
$count = $result->rowCount();
for($i=0;$i<$count;$i+=1)
{
$objArray[] = $result->fetch(PDO::FETCH_CLASS | PDO::FETCH_CLASSTYPE,"classType");
}
Or similar construct.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=42664&edit=1