Home » Database » DB_DataObject » Bug #4968
count() doesn't work after setFetchMode(DB_FETCHMODE_ASSOC)
Details
| Request #4968 | count() doesn't work after setFetchMode(DB_FETCHMODE_ASSOC) |
|---|---|
| Submitted | 2005-08-02 10:35 UTC |
| From | albert at jool dot nl |
| Status | Closed |
| Package | DB_DataObject |
| PHP Version | 4.3.11 |
| OS | all |
| Roadmaps | (Not assigned) |
Comments
[2005-08-02 10:35 UTC] albert at jool dot nl
Description:
------------
Tested with DB_DataObject 1.7.15.
Test script:
---------------
Sample code doesn't work but gives an idea of reproducing it:
$person = DB_DataObject::factory('person');
$db =& $person->getDatabaseConnection();
$db->setFetchMode(DB_FETCHMODE_ASSOC);
$total = $person->count();
echo $total;
---
the bug is on line 1502-1503 of DataObject.php:
$l = $result->fetchRow();
return $l[0];
With DB_FETCHMODE_ASSOC, $l[0] doesn't contain anything, because the count result is stored in $l['DATAOBJECT_NUM']
Expected result:
----------------
$total should be the number of person rows in the db
Actual result:
--------------
'' or null is returned.
[2005-08-02 11:59 UTC] albert at jool dot nl
simple solution, no risk:
change line 1502 from
$l = $result->fetchRow();
to
$l = $result->fetchRow(DB_FETCHMODE_ORDERED);
[2005-08-02 12:03 UTC] albert at jool dot nl
line 1502 from DataObject.php of course :)
and yes, I didn't expect that the count function would be influenced by changing fetch methods, especially when you're using the $db object for other queries.