Skip to content

Commit

Permalink
Add ability set number of items per page on PDO source
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan Hall committed Aug 8, 2017
1 parent 8df6e51 commit 1535b76
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/Objects/Sources/PDOSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class PDOSource implements SourceInterface
private $fields = [];
private $overrideSQL;
private $joins = [];
private $perPage = 10;

public function __construct(PDO $pdo, $tableName) {
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Expand Down Expand Up @@ -62,6 +63,10 @@ public function setOverrideSQL($overrideSQL) {
$this->fields = $this->getTableFields();
}

public function setPerPage($perPage = 10) {
$this->perPage = $perPage;
}

private function getSQL($fieldsToRetrieve) {

$fieldsSQL = implode(', ', $fieldsToRetrieve);
Expand All @@ -80,21 +85,19 @@ private function getSQL($fieldsToRetrieve) {
return $sql;
}

private function bindLimitParameters(PDOStatement $stmt, $offset, $perPage) {
private function bindLimitParameters(PDOStatement $stmt, $offset) {
$stmt->bindValue(1, $offset, PDO::PARAM_INT);
$stmt->bindValue(2, $perPage, PDO::PARAM_INT);
$stmt->bindValue(2, $this->perPage, PDO::PARAM_INT);
}

public function getDataRows($page = 1, $fieldsToRetrieve = []) {

$perPage = 10;

$offset = (($page-1) * $perPage);
$offset = (($page-1) * $this->perPage);

$sql = $this->getSQL($fieldsToRetrieve);

$stmt = $this->pdo->prepare($sql);
$this->bindLimitParameters($stmt, $offset, $perPage);
$this->bindLimitParameters($stmt, $offset);

$stmt->execute();

Expand Down

0 comments on commit 1535b76

Please sign in to comment.