PEAR is archived and read-only

This mirror preserves historical PEAR package releases and metadata so existing references remain available.

Home » Database » DB » Bug #232

getAssoc ignores $fetchMode

Details

Submitted2003-11-12 20:04 UTC
Frommjohnson at pitsco dot com
Assigneddanielc
StatusClosed
PackageDB
PHP VersionIrrelevant
OSLinux
Roadmaps(Not assigned)

Comments

[2003-11-12 20:04 UTC] mjohnson at pitsco dot com

Description:
------------
Retrieving an associative array of objects doesn't seem to work. getAssoc() doesn't appear to acknowledge the DB_FETCHMODE_OBJECT fetch mode. I don't know if this is a bug or intended. If it is intended, it should be documented, but I believe it really is a bug.

I'm using a MySQL database.

Reproduce code:
---------------
$query = "SELECT id, value1, value2 FROM aTable"
$res = $db->getAssoc($query, true, array(), DB_FETCHMODE_OBJECT);

Expected result:
----------------
$res = array('235' => Object set with value1 & value2 for 235,
'594' => Object set with value1 & value2 for 594)
);

Actual result:
--------------
$res = array('235' => array('value1' => 'Value1 for 235',
'value2' => 'Value2 for 235'),
'594' => array('value1' => 'Value1 for 594',
'value2' => 'Value2 for 594')
)

[2003-12-08 14:00 UTC] damien dot cassou at laposte dot net

I have the same bug, here are the sources :

function &getAssoc($query, $force_array = false, $params = array(),
$fetchmode = DB_FETCHMODE_ORDERED, $group = false)
{

...

if ($fetchmode == DB_FETCHMODE_ASSOC) {
....
} else {
....
}
...
}

We can see that :

1 - The default value is : DB_FETCHMODE_ORDERED instead of DB_FETCHMODE_DEFAULT

2 - Only DB_FETCHMODE_ASSOC is used not as a default.

[2004-01-15 18:41 UTC] redbeard at mdjohnson dot us

I would use it to populate an associative array with objects. Right now I'm using getAssoc() and then going through each element of the array and creating the object,
replacing the array that was there with the object.

The reason I'm doing this is that I need an associative array for easy look up of a group of objects. I'd use the current system but I need access to class methods for formatting output, updating the data, etc.

Michael