Advertisement
stuppid_bot

Untitled

Jun 13th, 2014
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.85 KB | None | 0 0
  1. <?php
  2.  
  3. class DB_Record extends Object {
  4.     // protected $db;
  5.     protected $table;
  6.     protected $primaryKey;
  7.     protected $fields;
  8.     // protected $copy;
  9.  
  10.     public function __construct($table) {
  11.         // $this->db = db::getConnection();
  12.         $this->table = $table;
  13.         $stm = db::query('DESCRIBE ' . $this->table);
  14.         $cols = $stm->fetchAll(PDO::FETCH_OBJ);
  15.         foreach ($cols as $col) {
  16.             if ($col->Key == 'PRI') {
  17.                 $this->primaryKey = $col->Field;
  18.             }
  19.             $this->fields[$col->Field] = $col->Default;
  20.         }
  21.     }
  22.  
  23.     public function load($id) {
  24.         if (preg_match('/^[1-9]\d*$/', $id)) {
  25.             $stm = db::query("SELECT * FROM `{$this->table}` WHERE `{$this->primaryKey}`=$id");
  26.             $data = $stm->fetch(PDO::FETCH_ASSOC);
  27.             if (count($data)) {
  28.                 $this->data = $data;
  29.                 $this->copy = $data;
  30.                 return true;
  31.             }
  32.         }
  33.         return false;
  34.     }
  35.  
  36.     public function save() {
  37.         // INSERT
  38.         if ($this->isNew()) {
  39.             $fields = $this->matchFields();
  40.             $sql = "INSERT INTO \"{$this->table}\"";
  41.             $sql .= ' ("' . implode('","', array_keys($fields)) . '"")';
  42.             $sql .= ' VALUES (' . substr(str_repeat('?,', count($fields)), 0 , -1) . ')';
  43.             echo $sql . '<br>';
  44.             $stm = db::prepare($sql);
  45.             $stm->execute(array_values($fields));
  46.             return db::lastInsertId();
  47.         }
  48.         // UPDATE
  49.         // if ($this->isChanged()) {
  50.             $fields = $this->matchFields();
  51.             $id = $fields[$this->primaryKey];
  52.             unset($fields[$this->primaryKey]);
  53.             $sql = "UPDATE `{$this->table}` SET ";
  54.             $parts = array();
  55.             foreach ($fields as $k => $v) {
  56.                 $parts[] = "`$k`=:$k";
  57.             }
  58.             $sql .= implode(',', $parts) . " WHERE `{$this->primaryKey}`=$id";
  59.             echo $sql . '<br>';
  60.             $stm = db::prepare($sql);
  61.             $stm->execute($fields);
  62.             return (bool) $stm->rowCount();
  63.             // return true;
  64.         // }
  65.         // return false;
  66.     }
  67.  
  68.     protected function isNew() {
  69.         return !$this->copy;
  70.     }
  71.  
  72.     // protected function isChanged() {
  73.     //     if ($this->copy) {
  74.     //         foreach ($this->copy as $k => $v) {
  75.     //             if (!isset($this->data[$k]) || $this->data[$k] !== $this->copy[$k]) {
  76.     //                 return true;
  77.     //             }
  78.     //         }
  79.     //     }
  80.     //     return false;
  81.     // }
  82.  
  83.     protected function matchFields() {
  84.         $ret = array();
  85.         foreach ($this->fields as $field => $default) {
  86.             $ret[$field] = isset($this->data[$field]) ? $this->data[$field] : $default;
  87.         }
  88.         return $ret;
  89.  
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement