PageRenderTime 21ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/class.dbobject.php

http://github.com/tylerhall/simple-php-framework
PHP | 232 lines | 183 code | 41 blank | 8 comment | 43 complexity | 632f6058870a08057c50a1293f9a6f1f MD5 | raw file
  1. <?PHP
  2. class DBObject
  3. {
  4. public $id;
  5. public $tableName;
  6. public $idColumnName;
  7. public $columns = array();
  8. protected function __construct($table_name, $columns, $id = null)
  9. {
  10. $this->tableName = $table_name;
  11. // A note on hardcoding $this->idColumnName = 'id'...
  12. // In many years working with this framework, I've used
  13. // a different id name exactly once - so I've decided to
  14. // drop the option from the constructor. You can overload
  15. // the constructor yourself if you have the need.
  16. $this->idColumnName = 'id';
  17. foreach($columns as $col) {
  18. $this->columns[$col] = null;
  19. }
  20. if(!is_null($id)) {
  21. $this->select($id);
  22. }
  23. }
  24. public function __get($key)
  25. {
  26. if(array_key_exists($key, $this->columns)) {
  27. return $this->columns[$key];
  28. }
  29. if((substr($key, 0, 2) == '__') && array_key_exists(substr($key, 2), $this->columns)) {
  30. return htmlspecialchars($this->columns[substr($key, 2)]);
  31. }
  32. $trace = debug_backtrace();
  33. trigger_error("Undefined property via DBObject::__get(): $key in {$trace[0]['file']} on line {$trace[0]['line']}", E_USER_NOTICE);
  34. return null;
  35. }
  36. public function __set($key, $value)
  37. {
  38. if(array_key_exists($key, $this->columns)) {
  39. $this->columns[$key] = $value;
  40. }
  41. return $value; // Seriously.
  42. }
  43. public function __unset($key)
  44. {
  45. unset($this->columns[$key]);
  46. }
  47. public function __isset($key)
  48. {
  49. return array_key_exists($key, $this->columns);
  50. }
  51. public function select($id, $column = null, $order_by = null, $sort_direction = 'DESC')
  52. {
  53. $db = Database::getDatabase();
  54. if(is_null($column)) $column = $this->idColumnName;
  55. $column = $db->escape($column);
  56. if(isset($order_by) && isset($sort_direction)) {
  57. $db->query("SELECT * FROM `{$this->tableName}` WHERE `$column` = :id ORDER BY `$order_by` $sort_direction LIMIT 1", array('id' => $id));
  58. } else {
  59. $db->query("SELECT * FROM `{$this->tableName}` WHERE `$column` = :id LIMIT 1", array('id' => $id));
  60. }
  61. if($db->hasRows())
  62. {
  63. $row = $db->getRow();
  64. $this->load($row);
  65. return true;
  66. }
  67. return false;
  68. }
  69. public function ok()
  70. {
  71. return !is_null($this->id);
  72. }
  73. public function save()
  74. {
  75. if(is_null($this->id))
  76. $this->insert();
  77. else
  78. $this->update();
  79. return $this->id;
  80. }
  81. public function insert($cmd = 'INSERT INTO')
  82. {
  83. $db = Database::getDatabase();
  84. if(count($this->columns) == 0) return false;
  85. $data = array();
  86. foreach($this->columns as $k => $v) {
  87. if(isset($v) && !is_null($v)) {
  88. if(is_bool($v)) {
  89. $data[$k] = $v ? 'TRUE' : 'FALSE';
  90. } else if(is_int($v) || is_float($v)) {
  91. $data[$k] = "$v";
  92. } else if(is_string($v)) {
  93. $data[$k] = $db->quote($v);
  94. }
  95. } else {
  96. $data[$k] = 'NULL';
  97. }
  98. }
  99. $columns = '`' . implode('`, `', array_keys($data)) . '`';
  100. $values = implode(',', $data);
  101. $db->query("$cmd `{$this->tableName}` ($columns) VALUES ($values)");
  102. $this->id = $db->insertID();
  103. return $this->id;
  104. }
  105. public function replace()
  106. {
  107. return $this->delete() && $this->insert();
  108. }
  109. public function update()
  110. {
  111. if(is_null($this->id)) return false;
  112. $db = Database::getDatabase();
  113. if(count($this->columns) == 0) return;
  114. $data = array();
  115. foreach($this->columns as $k => $v) {
  116. if(isset($v) && !is_null($v)) {
  117. if(is_bool($v)) {
  118. $data[$k] = $v ? 'TRUE' : 'FALSE';
  119. } else if(is_int($v) || is_float($v)) {
  120. $data[$k] = "$v";
  121. } else if(is_string($v)) {
  122. $data[$k] = $db->quote($v);
  123. }
  124. } else {
  125. $data[$k] = 'NULL';
  126. }
  127. }
  128. $sql = "UPDATE {$this->tableName} SET ";
  129. foreach($data as $k => $v) {
  130. $sql .= "`$k` = $v,";
  131. }
  132. $sql[strlen($sql) - 1] = ' ';
  133. $sql .= " WHERE `{$this->idColumnName}` = " . $db->quote($this->id);
  134. $db->query($sql);
  135. return $db->affectedRows();
  136. }
  137. public function delete()
  138. {
  139. if(is_null($this->id)) return false;
  140. $db = Database::getDatabase();
  141. $db->query("DELETE FROM `{$this->tableName}` WHERE `{$this->idColumnName}` = :id LIMIT 1", array('id' => $this->id));
  142. return $db->affectedRows();
  143. }
  144. public function load($row)
  145. {
  146. foreach($row as $k => $v)
  147. {
  148. if($k == $this->idColumnName)
  149. $this->id = $v;
  150. elseif(array_key_exists($k, $this->columns))
  151. $this->columns[$k] = $v;
  152. }
  153. }
  154. // Grabs a large block of instantiated $class_name objects from the database using only one query.
  155. public static function glob($sql = null, $extra_columns = array())
  156. {
  157. $db = Database::getDatabase();
  158. // Make sure the class exists before we instantiate it...
  159. $class_name = get_called_class();
  160. if(!class_exists($class_name)) {
  161. return false;
  162. }
  163. $tmp_obj = new $class_name;
  164. // Also, it needs to be a subclass of DBObject...
  165. if(!is_subclass_of($tmp_obj, 'DBObject')) {
  166. return false;
  167. }
  168. if(is_null($sql)) {
  169. $sql = "SELECT * FROM `{$tmp_obj->tableName}`";
  170. }
  171. $objs = array();
  172. $rows = $db->getRows($sql);
  173. foreach($rows as $row)
  174. {
  175. $o = new $class_name;
  176. $o->load($row);
  177. $objs[$o->id] = $o;
  178. foreach($extra_columns as $c)
  179. {
  180. $o->addColumn($c);
  181. $o->$c = isset($row[$c]) ? $row[$c] : null;
  182. }
  183. }
  184. return $objs;
  185. }
  186. public function addColumn($key, $val = null)
  187. {
  188. if(!in_array($key, array_keys($this->columns)))
  189. $this->columns[$key] = $val;
  190. }
  191. }