Advertisement
stuppid_bot

Record.php

Jun 7th, 2014
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.67 KB | None | 0 0
  1. <?php
  2.  
  3. class Record {
  4.     protected $_table;
  5.     protected $_primaryKey = 'id';
  6.     protected $_data = array();
  7.  
  8.     public function __construct($table = null, $primary_key = null)
  9.     {
  10.         if (!is_null($table)) {
  11.             $this->setTable($table);
  12.         }
  13.  
  14.         if (!is_null($primary_key)) {
  15.             $this->setPrimaryKey($primary_key);
  16.         }
  17.     }
  18.  
  19.     public function setTable($table)
  20.     {
  21.         $this->_table = $table;
  22.     }
  23.  
  24.     public function setPrimaryKey($primary_key)
  25.     {
  26.         $this->_primaryKey = $primary_key;
  27.     }
  28.  
  29.     public function load($id, $field = null)
  30.     {
  31.         $q = new Query($this->_table);
  32.         $q->where($field ? $field : $this->_primaryKey, $id);
  33.         $data = $q->row();
  34.  
  35.         if ($data) {
  36.             $this->_data = $data;
  37.             return true;
  38.         }
  39.  
  40.         return false;
  41.     }
  42.  
  43.     public function save()
  44.     {
  45.         $q = new Query($this->_table);
  46.  
  47.         if (empty($this->{$this->_primaryKey})) {
  48.             return $q->insert($this->_data);
  49.         }
  50.  
  51.         $q->where($this->_primaryKey, $this->{$this->_primaryKey});
  52.         return $q->update($this->_data);
  53.     }
  54.  
  55.     public function to_a()
  56.     {
  57.         return $this->_data;
  58.     }
  59.  
  60.     public function __get($k)
  61.     {
  62.         return $this->__isset($k) ? $this->_data[$k] : null;
  63.     }
  64.  
  65.     public function __set($k, $v)
  66.     {
  67.         return $this->_data[$k] = $v;
  68.     }
  69.  
  70.     public function __isset($k)
  71.     {
  72.         return array_key_exists($k, $this->_data);
  73.     }
  74.  
  75.     public function __unset($k)
  76.     {
  77.         if ($this->__isset($k)) {
  78.             unset($this->_data[$k]);
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement