Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Db
- {
- private $pdo = null;
- private static $instance = null;
- private static $sql_host = 'localhost';
- private static $sql_base = 'demo';
- private static $sql_user = 'root';
- private static $sql_password = 'root';
- private function __construct()
- {
- $options = [
- \PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'",
- \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC
- ];
- $this->pdo = new \PDO('mysql:host=' . self::$sql_host . ';dbname=' . self::$sql_base, self::$sql_user, self::$sql_password, $options);
- }
- public static function get()
- {
- if (is_null(self::$instance))
- {
- self::$instance = new self;
- }
- return self::$instance;
- }
- public function __call($method, $arguments)
- {
- if (method_exists($this->pdo, $method) === true)
- {
- return call_user_func_array([$this->pdo, $method], $arguments);
- }
- }
- }
- abstract class Entity
- {
- public $fields = [];
- public $pk = 'id';
- public $table = '';
- public function all()
- {
- $query = "SELECT * FROM {$this->table} ORDER BY id DESC";
- $statement = Db::get()->query($query);
- return $statement->rowCount() > 0 ? $statement->fetchAll(PDO::FETCH_ASSOC) : false;
- }
- public function get($id)
- {
- $query = "SELECT * FROM {$this->table} WHERE {$this->pk} = $id LIMIT 1";
- $statement = Db::get()->query($query);
- return $statement->rowCount() == 1 ? $statement->fetch() : false;
- }
- public function add($data)
- {
- $cols = implode(',', array_keys($data));
- $query = "INSERT INTO {$this->table}($cols) VALUES (";
- foreach ($data as $key => $value) {
- if (in_array($key, $this->fields)) {
- $query .= ":$key,";
- }
- }
- reset($data);
- $query = substr($query, 0, -1);
- $query .= ")";
- $statement = Db::get()->prepare($query);
- foreach ($data as $key => $value) {
- if (in_array($key, $this->fields)) {
- $statement->bindValue(':'.$key, $value);
- }
- }
- $result = $statement->execute();
- return $result;
- }
- public function update($id, $data = [])
- {
- $query = "UPDATE {$this->table} SET ";
- foreach ($data as $key => $value) {
- if (in_array($key, $this->fields)) {
- $query .= "$key = :$key,";
- }
- }
- reset($data);
- $query = substr($query, 0, -1);
- $query .= " WHERE {$this->pk} = $id LIMIT 1";
- $statement = Db::get()->prepare($query);
- foreach ($data as $key => $value) {
- if (in_array($key, $this->fields)) {
- $statement->bindValue(':'.$key, $value);
- }
- }
- $result = $statement->execute();
- return $result;
- }
- public function delete($id)
- {
- $query = "DELETE FROM {$this->table} WHERE {$this->pk} = $id LIMIT 1";
- $result = Db::get()->exec($query);
- return $result;
- }
- public static function __callStatic($method, $args)
- {
- return (new static)->$method(...$args);
- }
- }
- class Post extends Entity
- {
- public $fields = [
- 'title',
- 'content',
- 'status'
- ];
- public $table = 'posts';
- }
- //Post::add(['title' => 'Jean', 'content' => 'Marie']);
- var_dump(Post::get(1));
- //var_dump(Post::update(1, ['title' => 'Bar']));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement