PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/code/classes/DAO/Base.class.php

https://github.com/blekkzor/pinetd2
PHP | 106 lines | 84 code | 21 blank | 1 comment | 7 complexity | d392f015a238babccc75acc0d8ad0481 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. namespace DAO;
  3. use \ArrayAccess;
  4. use \Exception;
  5. abstract class Base implements ArrayAccess {
  6. // DAO objects are singletons
  7. protected $table = null;
  8. protected $key = null;
  9. protected $SQL = null;
  10. public function __construct($table, $key) {
  11. if (is_null($this->SQL)) throw new Exception('Do not create a new DAO::Base directly, please choose a DB driver');
  12. $this->table = $table;
  13. $this->key = $key;
  14. }
  15. public function __clone() {
  16. throw new Exception('Cloning a DAO is not allowed');
  17. }
  18. abstract public function deleteBean($bean);
  19. abstract public function delete(array $where);
  20. abstract public function createUpdateQuery($data, $table, $qwhere);
  21. abstract public function insertValues($data);
  22. abstract public function createSelectQuery($qtype = 'SELECT', $qfields = '*', $qtable = null, $qwhere = null, $order_by = null, $limit = null);
  23. public function insert($data) {
  24. return $this->insertValues($data);
  25. }
  26. public function generateBean($data) {
  27. return new Bean($this, $data);
  28. }
  29. public function updateValues($bean) {
  30. $new_data = $bean->getUpdatedProperties();
  31. if (!$new_data) return true; // nothing to do
  32. $key = $this->key;
  33. $key_val = $bean->_PK;
  34. return $this->createUpdateQuery($new_data, $this->table, array(array($key, $key_val)));
  35. }
  36. public function loadByField($where_data, $order_by = null, $limit = null) {
  37. return $this->search($where_data, $order_by, $limit);
  38. }
  39. public function search($where_data, $order_by = null, $limit = null) {
  40. $result = $this->createSelectQuery('SELECT', '*', $this->table, $where_data, $order_by, $limit);
  41. if (!is_object($result)) return null;
  42. $Bean = array();
  43. while($row = $result->fetch_assoc())
  44. $Bean[] = $this->generateBean($row);
  45. $result->close();
  46. return $Bean;
  47. }
  48. public function searchOne($where_data) {
  49. $res = $this->search($where_data, null, array(1));
  50. return $res[0];
  51. }
  52. public function loadLast() {
  53. $res = $this->search(array($this->key => NULL));
  54. if (!$res) return null;
  55. return $res[0];
  56. }
  57. public function countByField($where_data, $order_by = null, $limit = null) {
  58. $result = $this->createSelectQuery('SELECT', 'COUNT(1)', $this->table, $where_data, $order_by, $limit);
  59. if (!is_object($result)) return null;
  60. $result = $result->fetch_row();
  61. return $result[0];
  62. }
  63. public function loadFromId($id) {
  64. list($result) = $this->search(array($this->key => $id));
  65. return $result;
  66. }
  67. public function getKey() {
  68. return $this->key;
  69. }
  70. public function offsetExists($id) {
  71. $result = $this->createSelectQuery('SELECT', '1', $this->table, array($this->key => $id));
  72. if (!is_object($result)) return null;
  73. if ($result->num_rows < 1) return false;
  74. return true;
  75. }
  76. public function offsetGet($id) {
  77. return $this->loadFromId($id);
  78. }
  79. public function offsetSet($id, $val) {
  80. throw new Exception('Can\'t directly set value for a primary key!');
  81. }
  82. public function offsetUnset($id) {
  83. throw new Exception('Not implemented yet'); // TODO: implement deletion by primary key
  84. }
  85. }