Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. <?php
  2.  
  3. class Db
  4. {
  5. private $pdo = null;
  6. private static $instance = null;
  7. private static $sql_host = 'localhost';
  8. private static $sql_base = 'demo';
  9. private static $sql_user = 'root';
  10. private static $sql_password = 'root';
  11.  
  12. private function __construct()
  13. {
  14. $options = [
  15. \PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'",
  16. \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC
  17. ];
  18.  
  19. $this->pdo = new \PDO('mysql:host=' . self::$sql_host . ';dbname=' . self::$sql_base, self::$sql_user, self::$sql_password, $options);
  20. }
  21.  
  22. public static function get()
  23. {
  24. if (is_null(self::$instance))
  25. {
  26. self::$instance = new self;
  27. }
  28.  
  29. return self::$instance;
  30. }
  31.  
  32. public function __call($method, $arguments)
  33. {
  34. if (method_exists($this->pdo, $method) === true)
  35. {
  36. return call_user_func_array([$this->pdo, $method], $arguments);
  37. }
  38. }
  39. }
  40.  
  41. abstract class Entity
  42. {
  43. public $fields = [];
  44.  
  45. public $pk = 'id';
  46.  
  47. public $table = '';
  48.  
  49. public function all()
  50. {
  51. $query = "SELECT * FROM {$this->table} ORDER BY id DESC";
  52. $statement = Db::get()->query($query);
  53.  
  54. return $statement->rowCount() > 0 ? $statement->fetchAll(PDO::FETCH_ASSOC) : false;
  55. }
  56.  
  57. public function get($id)
  58. {
  59. $query = "SELECT * FROM {$this->table} WHERE {$this->pk} = $id LIMIT 1";
  60. $statement = Db::get()->query($query);
  61.  
  62. return $statement->rowCount() == 1 ? $statement->fetch() : false;
  63. }
  64.  
  65. public function add($data)
  66. {
  67. $cols = implode(',', array_keys($data));
  68. $query = "INSERT INTO {$this->table}($cols) VALUES (";
  69.  
  70. foreach ($data as $key => $value) {
  71. if (in_array($key, $this->fields)) {
  72. $query .= ":$key,";
  73. }
  74. }
  75.  
  76. reset($data);
  77.  
  78. $query = substr($query, 0, -1);
  79. $query .= ")";
  80.  
  81. $statement = Db::get()->prepare($query);
  82.  
  83. foreach ($data as $key => $value) {
  84. if (in_array($key, $this->fields)) {
  85. $statement->bindValue(':'.$key, $value);
  86. }
  87. }
  88.  
  89. $result = $statement->execute();
  90.  
  91. return $result;
  92. }
  93.  
  94. public function update($id, $data = [])
  95. {
  96. $query = "UPDATE {$this->table} SET ";
  97.  
  98. foreach ($data as $key => $value) {
  99. if (in_array($key, $this->fields)) {
  100. $query .= "$key = :$key,";
  101. }
  102. }
  103.  
  104. reset($data);
  105.  
  106. $query = substr($query, 0, -1);
  107. $query .= " WHERE {$this->pk} = $id LIMIT 1";
  108.  
  109. $statement = Db::get()->prepare($query);
  110.  
  111. foreach ($data as $key => $value) {
  112. if (in_array($key, $this->fields)) {
  113. $statement->bindValue(':'.$key, $value);
  114. }
  115. }
  116.  
  117. $result = $statement->execute();
  118.  
  119. return $result;
  120. }
  121.  
  122. public function delete($id)
  123. {
  124. $query = "DELETE FROM {$this->table} WHERE {$this->pk} = $id LIMIT 1";
  125. $result = Db::get()->exec($query);
  126.  
  127. return $result;
  128. }
  129.  
  130. public static function __callStatic($method, $args)
  131. {
  132. return (new static)->$method(...$args);
  133. }
  134. }
  135.  
  136. class Post extends Entity
  137. {
  138. public $fields = [
  139. 'title',
  140. 'content',
  141. 'status'
  142. ];
  143.  
  144. public $table = 'posts';
  145. }
  146.  
  147. //Post::add(['title' => 'Jean', 'content' => 'Marie']);
  148. var_dump(Post::get(1));
  149. //var_dump(Post::update(1, ['title' => 'Bar']));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement