Skip to content

Commit

Permalink
Fix FETCHMODE_OBJECT with prepared statement and use iterator_to_arra…
Browse files Browse the repository at this point in the history
…y() in PearCompat::getAll()
  • Loading branch information
Olivier Poitrey committed Nov 13, 2008
1 parent b9ffe78 commit 191cbb4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
9 changes: 2 additions & 7 deletions MyDBD/PearCompat.php
Expand Up @@ -104,12 +104,7 @@ static public function getAll(MyDBD $dbh, $query, $params = array(), $fetchmode
if ($fetchmode == DB_FETCHMODE_DEFAULT) $fetchmode = DB_FETCHMODE_ORDERED;
$res = $dbh->query($query, $params);
$res->setFetchMode($fetchmode);
$result = array();
foreach ($res as $row)
{
$result[] = $row;
}
return $result;
return iterator_to_array($res);
}

/**
Expand Down Expand Up @@ -208,4 +203,4 @@ static public function getAssoc(MyDBD $dbh, $query, $forceArray = false, $params

return $results;
}
}
}
6 changes: 2 additions & 4 deletions MyDBD/ResultSet.php
Expand Up @@ -16,10 +16,8 @@ class MyDBD_ResultSet implements SeekableIterator, Countable
FETCHMODE_OBJECT = 3;

protected
$result = null,
$options = null;

private
$result = null,
$options = null,
$cursor = 0,
$fetchMode = self::FETCHMODE_ORDERED,
$fetchClass = null;
Expand Down
16 changes: 14 additions & 2 deletions MyDBD/StatementResultSet.php
Expand Up @@ -86,11 +86,23 @@ protected function fetchAsObject()
{
if ($this->result->fetch())
{
$obj = new stdClass();
$assoc = array();
$fieldNames = $this->getFieldNames();

for ($i = 0; $i < count($this->boundData); $i++)
{
$obj[$fieldNames[$i]] = $this->boundData[$i];
$assoc[$fieldNames[$i]] = $this->boundData[$i];
}

if ($this->fetchClass == 'stdClass')
{
return (object) $assoc;
}
else
{
// NOTE: This is not equivalent to mysqli_fetch_object()
$className = $this->fetchClass;
return new $className($assoc);
}
}
else
Expand Down

0 comments on commit 191cbb4

Please sign in to comment.