diff --git a/src/Dibi/Result.php b/src/Dibi/Result.php index 73f2cd40e..1cababe63 100644 --- a/src/Dibi/Result.php +++ b/src/Dibi/Result.php @@ -235,7 +235,8 @@ final public function fetchAssoc(string $assoc): array } $this->seek(0); - $row = $this->fetch(); + $driver = $this->getResultDriver(); + $row = $driver->fetch(true); if (!$row) { return []; // empty result set } @@ -246,7 +247,7 @@ final public function fetchAssoc(string $assoc): array // check columns foreach ($assoc as $as) { // offsetExists ignores null in PHP 5.2.1, isset() surprisingly null accepts - if ($as !== '[]' && $as !== '=' && $as !== '->' && $as !== '|' && !property_exists($row, $as)) { + if ($as !== '[]' && $as !== '=' && $as !== '->' && $as !== '|' && !array_key_exists($as, $row)) { throw new \InvalidArgumentException("Unknown column '$as' in associative descriptor."); } } @@ -261,6 +262,7 @@ final public function fetchAssoc(string $assoc): array // make associative tree do { + $this->normalize($row); $x = &$data; // iterative deepening @@ -269,12 +271,12 @@ final public function fetchAssoc(string $assoc): array $x = &$x[]; } elseif ($as === '=') { // "value" node - $x = $row->{$assoc[$i + 1]}; + $x = $row[$assoc[$i + 1]]; continue 2; } elseif ($as === '->') { // "object" node if ($x === null) { - $x = clone $row; + $x = new $this->rowClass($row); $x = &$x->{$assoc[$i + 1]}; $x = null; // prepare child node } else { @@ -282,14 +284,20 @@ final public function fetchAssoc(string $assoc): array } } elseif ($as !== '|') { // associative-array node - $x = &$x[$row->$as]; + $x = &$x[$row[$as]]; } } if ($x === null) { // build leaf - $x = $row; + if ($this->rowFactory) { + $x = ($this->rowFactory)($row); + } elseif ($this->rowClass) { + $x = new $this->rowClass($row); + } else { + $x = $row; + } } - } while ($row = $this->fetch()); + } while ($row = $driver->fetch(true)); unset($x); return $data; @@ -300,7 +308,8 @@ final public function fetchAssoc(string $assoc): array private function oldFetchAssoc(string $assoc) { $this->seek(0); - $row = $this->fetch(); + $driver = $this->getResultDriver(); + $row = $driver->fetch(true); if (!$row) { return []; // empty result set } @@ -323,6 +332,7 @@ private function oldFetchAssoc(string $assoc) } do { + $this->normalize($row); $x = &$data; foreach ($assoc as $i => $as) { @@ -331,7 +341,7 @@ private function oldFetchAssoc(string $assoc) } elseif ($as === '=') { // "record" node if ($x === null) { - $x = $row->toArray(); + $x = $row; $x = &$x[$assoc[$i + 1]]; $x = null; // prepare child node } else { @@ -340,7 +350,7 @@ private function oldFetchAssoc(string $assoc) } elseif ($as === '@') { // "object" node if ($x === null) { - $x = clone $row; + $x = new $this->rowClass($row); $x = &$x->{$assoc[$i + 1]}; $x = null; // prepare child node } else { @@ -348,18 +358,22 @@ private function oldFetchAssoc(string $assoc) } } else { // associative-array node - $x = &$x[$row->$as]; + $x = &$x[$row[$as]]; } } if ($x === null) { // build leaf if ($leaf === '=') { - $x = $row->toArray(); + $x = $row; + } elseif ($this->rowFactory) { + $x = ($this->rowFactory)($row); + } elseif ($this->rowClass) { + $x = new $this->rowClass($row); } else { $x = $row; } } - } while ($row = $this->fetch()); + } while ($row = $driver->fetch(true)); unset($x); return $data;