SqlTableModel.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace KarmaFW\Database\Sql;
  3. use \KarmaFW\App;
  4. class SqlTableModel
  5. {
  6. public static $table_name = '';
  7. public static $primary_key = [];
  8. protected static $default_item = [];
  9. public static function getDb()
  10. {
  11. return App::getDb();
  12. }
  13. public static function checkTable()
  14. {
  15. if (empty(static::$table_name)) {
  16. throw new \Exception("no table_name defined in " . get_called_class(), 1);
  17. }
  18. }
  19. public static function all($where=[], $options=[])
  20. {
  21. // Alias of getAll
  22. return static::getAll($where, $options);
  23. }
  24. public static function select($where=[], $options=[])
  25. {
  26. // Alias of getAll
  27. return static::getAll($where, $options);
  28. }
  29. public static function selectAll($where=[], $options=[])
  30. {
  31. // Alias of getAll
  32. return static::getAll($where, $options);
  33. }
  34. public static function get($where=[], $options=[])
  35. {
  36. // Alias of getAll
  37. return static::getAll($where, $options);
  38. }
  39. public static function getAll($where=[], $options=[])
  40. {
  41. $db = static::getDb();
  42. static::checkTable();
  43. return $db->getTable(static::$table_name)->select($where, $options);
  44. }
  45. public static function selectCount($where=[], $options=[])
  46. {
  47. $db = static::getDb();
  48. static::checkTable();
  49. return $db->getTable(static::$table_name)->selectCount($where, $options);
  50. }
  51. public static function getAllPagination($where=null, $options=[])
  52. {
  53. $db = static::getDb();
  54. static::checkTable();
  55. $tuple = $db->getTable(static::$table_name)->getAllWithFoundRows($where, $options);
  56. //list($found_rows, $data) = $tuple;
  57. return $tuple;
  58. }
  59. public static function one($where=[], $options=[])
  60. {
  61. // Alias of getOne
  62. return static::getOne($where, $options);
  63. }
  64. public static function selectOne($where=[], $options=[])
  65. {
  66. // Alias of getOne
  67. return static::getOne($where, $options);
  68. }
  69. public static function getOne($where=[], $options=[])
  70. {
  71. $db = static::getDb();
  72. static::checkTable();
  73. return $db->getTable(static::$table_name)->selectOne($where, $options);
  74. }
  75. public static function insert($values=[], $options=[])
  76. {
  77. $db = static::getDb();
  78. static::checkTable();
  79. return $db->getTable(static::$table_name)->insert($values, $options);
  80. }
  81. public static function insertAll($rows=[], $options=[])
  82. {
  83. $db = static::getDb();
  84. static::checkTable();
  85. return $db->getTable(static::$table_name)->insertAll($rows, $options);
  86. }
  87. public static function insertSelect($insert_table, $insert_keys=null, $rows=[], $options=[])
  88. {
  89. $db = static::getDb();
  90. static::checkTable();
  91. return $db->getTable(static::$table_name)->insertSelect($insert_table, $insert_keys, $rows, $options);
  92. }
  93. public static function update($updates=[], $where=[], $options=[])
  94. {
  95. $db = static::getDb();
  96. static::checkTable();
  97. return $db->getTable(static::$table_name)->update($updates, $where, $options);
  98. }
  99. public static function delete($where=[], $options=[])
  100. {
  101. $db = static::getDb();
  102. static::checkTable();
  103. return $db->getTable(static::$table_name)->delete($where, $options);
  104. }
  105. // load a table row by his primary key. (usage: `$user = User::load($user_id);` )
  106. public static function load($pk_where=[], $where=[], $options=[])
  107. {
  108. if (empty($pk_where)) {
  109. return null;
  110. }
  111. if (count(static::$primary_key) == 1 && is_scalar($pk_where)) {
  112. $pk_where = [ static::$primary_key[0] => $pk_where ];
  113. }
  114. if (empty(static::$primary_key)) {
  115. throw new \Exception("no primary_key defined in " . get_called_class(), 1);
  116. }
  117. if (empty($where)) {
  118. $where = [];
  119. }
  120. foreach (static::$primary_key as $key) {
  121. if (! isset($pk_where[$key])) {
  122. throw new \Exception("missing value for primary_key " . $key . " in " . get_called_class(), 1);
  123. }
  124. $value = $pk_where[$key];
  125. $where[$key] = $value;
  126. }
  127. return static::getOne($where, $options);
  128. }
  129. public static function getDefaultItem()
  130. {
  131. return array_slice(static::$default_item, 0);
  132. }
  133. }