|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2017-10-16 10:25 UTC] aljosha dot papsch at vinexus dot eu
Description:
------------
I created a small wrapper around PDO. My first stab at wrapping fetchAll looked like this:
public function fetchAll($fetchStyle = \PDO::FETCH_BOTH,
$fetchArgument = null,
array $constructorArgs) {
return $this->st->fetchAll($fetchStyle, $fetchArgument, $constructorArgs);
}
PDO rejected the method call, complaining:
SQLSTATE[HY000]: General error: Extraneous additional parameters
So I had to turn the simple one liner into:
public function fetchAll($fetchStyle = \PDO::FETCH_BOTH,
$fetchArgument = null,
array $constructorArgs = []) {
if ($fetchStyle === \PDO::FETCH_BOTH) {
return $this->st->fetchAll();
}
elseif ($fetchStyle === \PDO::FETCH_CLASS) {
return $this->st->fetchAll($fetchStyle, $fetchArgument, $constructorArgs);
}
elseif ($fetchStyle === \PDO::FETCH_ASSOC ||
$fetchStyle === \PDO::FETCH_NUM ||
$fetchStyle === \PDO::FETCH_ASSOC) {
return $this->st->fetchAll($fetchStyle);
}
else {
return $this->st->fetchAll($fetchStyle, $fetchArgument);
}
}
It would be great if PDO would not complain about extraneous arguments and just ignore arguments not needed. Otherwise, any PDO wrapper has to do this logic again of its own, possibly with errors. For example, there is a bug in the code above: I forgot considering \PDO::FETCH_OBJ in the last elseif block.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Dec 20 04:00:01 2025 UTC |
Simple subclassing of PDOStatement and overriding of fetchAll(...) is broken. Necessitating code like such: function fetchAll($how = null, $class_name = null, $ctor_args = null) { if (!isset($ctor_args)) { if (!isset($class_name)) { if (!isset($how)) { return $this->PDOStatement->fetchAll(); } return $this->PDOStatement->fetchAll($how); } return $this->PDOStatement->fetchAll($how, $class_name); } return $this->PDOStatement->fetchAll($how, $class_name, $ctor_args); }