php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #42664 PDO::FETCH_CLASSTYPE is not useful for abstracting code.
Submitted: 2007-09-13 18:37 UTC Modified: 2021-09-29 16:02 UTC
Votes:4
Avg. Score:4.5 ± 0.9
Reproduced:2 of 3 (66.7%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: jdriddle_producer at yahoo dot como Assigned: cmb (profile)
Status: Wont fix Package: PDO related
PHP Version: 5.2.4 OS: *
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: jdriddle_producer at yahoo dot como
New email:
PHP Version: OS:

 

 [2007-09-13 18:37 UTC] 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.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-09-13 19:29 UTC] 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.
 [2011-02-21 21:01 UTC] jani@php.net
-Package: Feature/Change Request +Package: PDO related -Operating System: all +Operating System: *
 [2011-09-12 21:09 UTC] 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";
 [2021-09-29 16:02 UTC] cmb@php.net
-Status: Open +Status: Wont fix -Assigned To: +Assigned To: cmb
 [2021-09-29 16:02 UTC] cmb@php.net
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>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 11:01:30 2024 UTC