Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Record {
- protected $_table;
- protected $_primaryKey = 'id';
- protected $_data = array();
- public function __construct($table = null, $primary_key = null)
- {
- if (!is_null($table)) {
- $this->setTable($table);
- }
- if (!is_null($primary_key)) {
- $this->setPrimaryKey($primary_key);
- }
- }
- public function setTable($table)
- {
- $this->_table = $table;
- }
- public function setPrimaryKey($primary_key)
- {
- $this->_primaryKey = $primary_key;
- }
- public function load($id, $field = null)
- {
- $q = new Query($this->_table);
- $q->where($field ? $field : $this->_primaryKey, $id);
- $data = $q->row();
- if ($data) {
- $this->_data = $data;
- return true;
- }
- return false;
- }
- public function save()
- {
- $q = new Query($this->_table);
- if (empty($this->{$this->_primaryKey})) {
- return $q->insert($this->_data);
- }
- $q->where($this->_primaryKey, $this->{$this->_primaryKey});
- return $q->update($this->_data);
- }
- public function to_a()
- {
- return $this->_data;
- }
- public function __get($k)
- {
- return $this->__isset($k) ? $this->_data[$k] : null;
- }
- public function __set($k, $v)
- {
- return $this->_data[$k] = $v;
- }
- public function __isset($k)
- {
- return array_key_exists($k, $this->_data);
- }
- public function __unset($k)
- {
- if ($this->__isset($k)) {
- unset($this->_data[$k]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement