SqlTableModel.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 tableExists()
  20. {
  21. $db = static::getDb();
  22. static::checkTable();
  23. return $db->getTable(static::$table_name)->exists();
  24. }
  25. public static function getEmpty()
  26. {
  27. $db = static::getDb();
  28. static::checkTable();
  29. return $db->getTable(static::$table_name)->getEmpty();
  30. }
  31. public static function all($where=[], $options=[])
  32. {
  33. // Alias of getAll
  34. return static::getAll($where, $options);
  35. }
  36. public static function select($where=[], $options=[])
  37. {
  38. // Alias of getAll
  39. return static::getAll($where, $options);
  40. }
  41. public static function selectAll($where=[], $options=[])
  42. {
  43. // Alias of getAll
  44. return static::getAll($where, $options);
  45. }
  46. public static function get($where=[], $options=[])
  47. {
  48. // Alias of getAll
  49. return static::getAll($where, $options);
  50. }
  51. public static function getAll($where=[], $options=[])
  52. {
  53. $db = static::getDb();
  54. static::checkTable();
  55. return $db->getTable(static::$table_name)->getAll($where, $options);
  56. }
  57. public static function count($where=[], $options=[])
  58. {
  59. // Alias of selectCount
  60. return self::selectCount($where, $options);
  61. }
  62. public static function selectCount($where=[], $options=[])
  63. {
  64. $db = static::getDb();
  65. static::checkTable();
  66. return $db->getTable(static::$table_name)->selectCount($where, $options);
  67. }
  68. public static function getAllWithFoundRows($where=null, $options=[])
  69. {
  70. $db = static::getDb();
  71. static::checkTable();
  72. $tuple = $db->getTable(static::$table_name)->getAllWithFoundRows($where, $options);
  73. return $tuple;
  74. }
  75. public static function getAllPagination($where=null, $nb_per_page=10, $page_idx=1, $options=[])
  76. {
  77. $db = static::getDb();
  78. static::checkTable();
  79. $tuple = $db->getTable(static::$table_name)->getAllPagination($where, $nb_per_page, $page_idx, $options);
  80. return $tuple;
  81. }
  82. public static function one($where=[], $options=[])
  83. {
  84. // Alias of getOne
  85. return static::getOne($where, $options);
  86. }
  87. public static function selectOne($where=[], $options=[])
  88. {
  89. // Alias of getOne
  90. return static::getOne($where, $options);
  91. }
  92. public static function getOne($where=[], $options=[])
  93. {
  94. $db = static::getDb();
  95. static::checkTable();
  96. return $db->getTable(static::$table_name)->selectOne($where, $options);
  97. }
  98. public static function insert($values=[], $options=[])
  99. {
  100. $db = static::getDb();
  101. static::checkTable();
  102. return $db->getTable(static::$table_name)->insert($values, $options);
  103. }
  104. public static function insertAll($rows=[], $options=[])
  105. {
  106. $db = static::getDb();
  107. static::checkTable();
  108. return $db->getTable(static::$table_name)->insertAll($rows, $options);
  109. }
  110. public static function insertSelect($insert_table, $insert_keys=null, $rows=[], $options=[])
  111. {
  112. $db = static::getDb();
  113. static::checkTable();
  114. return $db->getTable(static::$table_name)->insertSelect($insert_table, $insert_keys, $rows, $options);
  115. }
  116. public static function update($updates=[], $where=[], $options=[])
  117. {
  118. $db = static::getDb();
  119. static::checkTable();
  120. return $db->getTable(static::$table_name)->update($updates, $where, $options);
  121. }
  122. public static function delete($where=[], $options=[])
  123. {
  124. $db = static::getDb();
  125. static::checkTable();
  126. return $db->getTable(static::$table_name)->delete($where, $options);
  127. }
  128. // load a table row by his primary key. (usage: `$user = User::load($user_id);` )
  129. public static function load($pk_where=[], $where=[], $options=[])
  130. {
  131. if (empty($pk_where)) {
  132. return null;
  133. }
  134. if (count(static::$primary_key) == 1 && is_scalar($pk_where)) {
  135. $pk_where = [ static::$primary_key[0] => $pk_where ];
  136. }
  137. if (empty(static::$primary_key)) {
  138. throw new \Exception("no primary_key defined in " . get_called_class(), 1);
  139. }
  140. if (! is_array($where)) {
  141. $where = [];
  142. }
  143. foreach (static::$primary_key as $key_idx => $key) {
  144. if (! array_key_exists($key, $pk_where)) {
  145. //if (count($pk_where) == count(static::$primary_key) && isset($pk_where[$key_idx])) {
  146. if (count($pk_where) == count(static::$primary_key) && array_key_exists($key_idx, $pk_where)) {
  147. //pre(static::$primary_key);
  148. $pk_where = array_combine(static::$primary_key, $pk_where);
  149. //pre($pk_where, 0, 'pk_where updated: ');
  150. } else {
  151. //pre($key_idx, 0, 'key_idx: ');
  152. //pre($pk_where, 0, 'pk_where KO: ');
  153. }
  154. }
  155. if (! array_key_exists($key, $pk_where)) {
  156. //exit;
  157. //pre(static::$primary_key); pre($pk_where, 1);
  158. throw new \Exception("missing value for primary_key " . $key . " in " . get_called_class(), 1);
  159. }
  160. $value = $pk_where[$key];
  161. $where[$key] = $value;
  162. }
  163. return static::getOne($where, $options);
  164. }
  165. public static function where($where=[])
  166. {
  167. static::checkTable();
  168. return (new WhereQuery(static::$table_name))->where($where);
  169. }
  170. /*
  171. public static function select($select=[])
  172. {
  173. static::checkTable();
  174. return (new WhereQuery(static::$table_name))->select($select);
  175. }
  176. */
  177. public static function getDefaultItem()
  178. {
  179. return array_slice(static::$default_item, 0);
  180. }
  181. }