Added fetchObject wrappers.
This commit is contained in:
parent
11eb84d315
commit
4a6061ec60
1 changed files with 34 additions and 0 deletions
|
@ -8,6 +8,7 @@ class DatabaseStatement {
|
|||
public $pdo;
|
||||
public $stmt;
|
||||
private $isQuery;
|
||||
private $hasExecuted = false;
|
||||
|
||||
public function __construct(PDOStatement $stmt, PDO $pdo, bool $isQuery) {
|
||||
$this->stmt = $stmt;
|
||||
|
@ -21,13 +22,26 @@ class DatabaseStatement {
|
|||
}
|
||||
|
||||
public function execute(array $params = []): bool {
|
||||
if($this->hasExecuted)
|
||||
return true;
|
||||
$this->hasExecuted = true;
|
||||
|
||||
return count($params) ? $this->stmt->execute($params) : $this->stmt->execute();
|
||||
}
|
||||
|
||||
public function executeGetId(array $params = []): int {
|
||||
if($this->hasExecuted)
|
||||
return true;
|
||||
$this->hasExecuted = true;
|
||||
|
||||
return $this->execute($params) ? $this->pdo->lastInsertId() : 0;
|
||||
}
|
||||
|
||||
public function reset(): DatabaseStatement {
|
||||
$this->hasExecuted = false;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function fetch($default = []) {
|
||||
$out = $this->isQuery || $this->execute() ? $this->stmt->fetch(PDO::FETCH_ASSOC) : false;
|
||||
return $out ? $out : $default;
|
||||
|
@ -42,4 +56,24 @@ class DatabaseStatement {
|
|||
$out = $this->isQuery || $this->execute() ? $this->stmt->fetchColumn($num) : false;
|
||||
return $out ? $out : $default;
|
||||
}
|
||||
|
||||
public function fetchObject(string $className = 'stdClass', ?array $args = null, $default = null) {
|
||||
$out = false;
|
||||
|
||||
if($this->isQuery || $this->execute()) {
|
||||
$out = $args === null ? $this->fetchObject($className) : $this->fetchObject($className, $args);
|
||||
}
|
||||
|
||||
return $out !== false ? $out : $default;
|
||||
}
|
||||
|
||||
public function fetchObjects(string $className = 'stdClass', ?array $args = null): array {
|
||||
$objects = [];
|
||||
|
||||
while(($object = $this->fetchObject($className, $args, false)) !== false) {
|
||||
$objects[] = $object;
|
||||
}
|
||||
|
||||
return $objects;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue