chrisenoch

DatabaseTable.php

Mar 29th, 2019
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. <?php
  2. namespace Ninja;
  3. class DatabaseTable
  4. {
  5. private $pdo;
  6. private $table;
  7. private $primaryKey;
  8. private $className;
  9. private $constructorArgs;
  10.  
  11. public function __construct(\PDO $pdo, string $table,
  12. string $primaryKey, string $className = '\stdClass',
  13. array $constructorArgs = []) {
  14. $this->pdo = $pdo;
  15. $this->table = $table;
  16. $this->primaryKey = $primaryKey;
  17. $this->className = $className;
  18. $this->constructorArgs = $constructorArgs;
  19. }
  20.  
  21. private function query($sql, $parameters = []) {
  22. $query = $this->pdo->prepare($sql);
  23. $query->execute($parameters);
  24. return $query;
  25. }
  26.  
  27. public function total() {
  28. $query = $this->query('SELECT COUNT(*)
  29. FROM `' . $this->table . '`');
  30. $row = $query->fetch();
  31. return $row[0];
  32. }
  33.  
  34. // TRY TO UNDERSTAND findById function better. Check previous version
  35. public function findById($value) {
  36. $query = 'SELECT * FROM `' . $this->table . '` WHERE `' .
  37. $this->primaryKey . '` = :value';
  38. $parameters = [
  39. 'value' => $value
  40. ];
  41. $query = $this->query($query, $parameters);
  42. return $query->fetchObject($this->className,
  43. $this->constructorArgs);
  44. }
  45.  
  46. public function findAll() {
  47. $result = $this->query('SELECT *
  48. FROM ' . $this->table);
  49. //return $result->fetchAll();
  50. return $result->fetchAll(\PDO::FETCH_CLASS, $this->className, $this->constructorArgs);
  51. }
  52.  
  53. public function find($column, $value) {
  54. $query = 'SELECT * FROM `' . $this->table . '` WHERE
  55. `' . $column . '` = :value';
  56. $parameters = [
  57. 'value' => $value
  58. ];
  59. $query = $this->query($query, $parameters);
  60. return $query->fetchAll(\PDO::FETCH_CLASS, $this->className, $this->constructorArgs);
  61. }
  62.  
  63. private function insert($fields) {
  64. $query = 'INSERT INTO `' . $this->table . '` (';
  65. foreach ($fields as $key => $value) {
  66. $query .= '`' . $key . '`,';
  67. }
  68. $query = rtrim($query, ',');
  69. $query .= ') VALUES (';
  70. foreach ($fields as $key => $value) {
  71. $query .= ':' . $key . ',';
  72. }
  73. $query = rtrim($query, ',');
  74. $query .= ')';
  75. $fields = $this->processDates($fields);
  76. $this->query($query, $fields);
  77. }
  78.  
  79. private function update($fields) {
  80. $query = ' UPDATE `' . $this->table .'` SET ';
  81. foreach ($fields as $key => $value) {
  82. $query .= '`' . $key . '` = :' . $key . ',';
  83. }
  84. $query = rtrim($query, ',');
  85. $query .= ' WHERE `' . $this->primaryKey . '`
  86. = :primaryKey';
  87. // Set the :primaryKey variable
  88. $fields['primaryKey'] = $fields['id'];
  89. $fields = $this->processDates($fields);
  90. $this->query($query, $fields);
  91. }
  92.  
  93. public function delete($id ) {
  94. $parameters = [':id' => $id];
  95. $this->query('DELETE FROM `' . $this->table . '` WHERE
  96. `' . $this->primaryKey . '` = :id', $parameters);
  97. }
  98.  
  99.  
  100. private function processDates($fields) {
  101. foreach ($fields as $key => $value) {
  102. if ($value instanceof \DateTime) {
  103. $fields[$key] = $value->format('Y-m-d');
  104. }
  105. }
  106. return $fields;
  107. }
  108.  
  109. public function save($record) {
  110. try {
  111. if ($record[$this->primaryKey] == '') {
  112. $record[$this->primaryKey] = null;
  113. }
  114. $this->insert($record);
  115. }
  116. catch (\PDOException $e) {
  117. $this->update($record);
  118. }
  119. }
  120.  
  121.  
  122.  
  123.  
  124. }
Add Comment
Please, Sign In to add comment