Edit report at https://bugs.php.net/bug.php?id=42664&edit=1

 ID:                 42664
 Comment by:         restlesslythought at gmail dot com
 Reported by:        jdriddle_producer at yahoo dot como
 Summary:            PDO::FETCH_CLASSTYPE is not useful for abstracting
                     code.
 Status:             Open
 Type:               Feature/Change Request
 Package:            PDO related
 Operating System:   *
 PHP Version:        5.2.4
 Block user comment: N
 Private report:     N

 New Comment:

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";


Previous Comments:
------------------------------------------------------------------------
[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

Reply via email to