1
0

model.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. <?php
  2. abstract class Model {
  3. protected static $table = "";
  4. protected static $id_field = "";
  5. protected static $fillable_fields = [];
  6. protected static $enforce_field_check = true;
  7. protected static $field_map = []; // Used for field name aliases
  8. protected static $object_fields = [];
  9. protected static $blob_fields = [];
  10. protected static $repositories = [];
  11. // Object variables
  12. protected $model_data;
  13. protected $modified_fields = [];
  14. protected $in_db;
  15. // Magic methods
  16. public function __get($name) {
  17. if(in_array($name, static::$object_fields)) {
  18. return $this->$name ?? null;
  19. }
  20. if(isset(static::$field_map[$name])) {
  21. $name = static::$field_map[$name];
  22. }
  23. return isset($this->model_data->$name) ? $this->model_data->$name : null;
  24. }
  25. public function __set($name, $value) {
  26. if(in_array($name, static::$object_fields)) {
  27. $this->$name = $value;
  28. return;
  29. }
  30. if(isset(static::$field_map[$name])) {
  31. $name = static::$field_map[$name];
  32. }
  33. $id_field = static::$id_field;
  34. if($name == "model_data") {
  35. $this->model_data = $value;
  36. }
  37. else if(in_array($name, static::$fillable_fields)) {
  38. if(!is_null($value)) {
  39. $value = strval($value);
  40. }
  41. if(!isset($this->model_data->$name) || ($this->model_data->$name !== $value)) {
  42. $this->model_data->$name = $value;
  43. $this->modified_fields[] = $name;
  44. }
  45. }
  46. else if($name == $id_field && empty($this->model_data->$id_field)) {
  47. $this->model_data->$id_field = $value;
  48. }
  49. else if(static::$enforce_field_check) {
  50. throw new Exception("Attempt to set an unfillable field $name");
  51. }
  52. else {
  53. $this->model_data->$name = $value;
  54. }
  55. }
  56. public function __isset($name) {
  57. $value = $this->$name;
  58. return !is_null($value);
  59. }
  60. // Static method
  61. public static function get($id) {
  62. $table = static::$table;
  63. $is_compound_key = is_array(static::$id_field);
  64. // Check repo for item
  65. if(!$is_compound_key && isset(static::$repositories[$table][$id])) {
  66. return static::$repositories[$table][$id];
  67. }
  68. $id_field = static::$id_field;
  69. $class = get_called_class();
  70. if($is_compound_key) { // Compound key
  71. $args = func_get_args();
  72. $fields = $id_field;
  73. if(count($args) != count($fields)) return false;
  74. foreach($fields as $index => $field) {
  75. $nvp[$field] = $args[$index];
  76. }
  77. }
  78. else {
  79. $nvp[$id_field] = $id;
  80. }
  81. $where = db::get_where_string($nvp, true);
  82. $sql = "SELECT * FROM `$table` WHERE $where";
  83. $result = db::query($sql);
  84. $object = db::object_or_false($result);
  85. if($object) {
  86. $object = new $class($object);
  87. if(!$is_compound_key) {
  88. static::$repositories[$table][$id] = $object;
  89. }
  90. }
  91. return $object;
  92. }
  93. public static function getOrException($id, $exception_message = false) {
  94. if(!$exception_message) $exception_message = get_called_class() . " not found.";
  95. $object = static::get($id);
  96. if(!$object) throw new Exception($exception_message);
  97. return $object;
  98. }
  99. public static function getAll() {
  100. $objects = [];
  101. $table = static::$table;
  102. $sql = "SELECT * FROM `$table`";
  103. $result = db::query($sql);
  104. while($row = db::fetch_object($result)) {
  105. $objects[] = new static($row);
  106. }
  107. return $objects;
  108. }
  109. public static function load(...$id_sets) {
  110. $table = static::$table;
  111. $ids = [];
  112. foreach($id_sets as $set) {
  113. $ids = array_merge($ids, $set);
  114. }
  115. if(empty($ids)) return [];
  116. $ids = array_unique($ids);
  117. $items = static::query([static::$id_field => $ids], ["id_as_key" => true]);
  118. foreach($items as $id => $item) {
  119. static::$repositories[$table][$id] = $item;
  120. }
  121. return $items;
  122. }
  123. public static function loadAll() {
  124. $table = static::$table;
  125. $items = static::query([], ["id_as_key" => true]);
  126. foreach($items as $id => $item) {
  127. static::$repositories[$table][$id] = $item;
  128. }
  129. return $items;
  130. }
  131. public static function deleteMultiple($nvp = []) {
  132. $table = static::$table;
  133. $vs = "1";
  134. if(!empty($nvp)) {
  135. $nvp = static::getRemappedNVP($nvp);
  136. $vs = db::get_where_string($nvp);
  137. }
  138. $objects = [];
  139. $sql = "DELETE FROM `$table` WHERE $vs";
  140. $result = db::query($sql);
  141. if(!$result) return false;
  142. return db::affected_rows();
  143. }
  144. public static function query($nvp = [], $options = []) {
  145. if(is_array($nvp)) {
  146. return static::findByNVP($nvp, $options);
  147. }
  148. else {
  149. return static::findByValueString($nvp, $options);
  150. }
  151. }
  152. public static function queryFuzzy($nvp = [], $options = []) {
  153. $options["fuzzy"] = true;
  154. return static::findByNVP($nvp, $options);
  155. }
  156. public static function queryOne($nvp = [], $options = []) {
  157. $rows = static::findByNVP($nvp, $options);
  158. return $rows ? $rows[0] : false;
  159. }
  160. private static function getRemappedNVP($nvp = []) {
  161. $mapped_nvp = [];
  162. foreach($nvp as $field => $value) {
  163. $field = static::$field_map[$field] ?? $field;
  164. $mapped_nvp[$field] = $value;
  165. }
  166. return $mapped_nvp;
  167. }
  168. public static function count($nvp = []) {
  169. $table = static::$table;
  170. $vs = "1";
  171. if(!empty($nvp)) {
  172. $nvp = static::getRemappedNVP($nvp);
  173. $vs = db::get_where_string($nvp);
  174. }
  175. $objects = [];
  176. $sql = "SELECT COUNT(*) AS `count` FROM `$table` WHERE $vs";
  177. $result = db::query($sql);
  178. $row = db::fetch_object($result);
  179. return $row->count;
  180. }
  181. public static function findByNVP($nvp, $options = []) {
  182. if(empty($nvp)) {
  183. $vs = "1";
  184. }
  185. else {
  186. $nvp = static::getRemappedNVP($nvp);
  187. $vs = db::get_where_string($nvp, $options);
  188. }
  189. return static::findByValueString($vs, $options);
  190. }
  191. private static function findByValueString($vs, $options = []) {
  192. $table = static::$table;
  193. $options = (object) $options;
  194. $objects = [];
  195. $sql = "SELECT * FROM `$table` WHERE $vs";
  196. if(isset($options->sort)) {
  197. $sort = static::$field_map[$options->sort] ?? $options->sort;
  198. $sql .= " ORDER BY `$sort`";
  199. if(isset($options->order)) {
  200. $sql .= " " . strtoupper($options->order);
  201. }
  202. }
  203. if(isset($options->limit)) {
  204. $sql .= " LIMIT";
  205. if(isset($options->offset)) {
  206. $sql .= " $options->offset,";
  207. }
  208. $sql .= " $options->limit";
  209. }
  210. $result = db::query($sql);
  211. while($row = db::fetch_object($result)) {
  212. $objects[] = new static($row);
  213. }
  214. if(!empty($options->id_as_key)) {
  215. $objects = Util::arrayFlipByID($objects);
  216. }
  217. return $objects;
  218. }
  219. // Constructor
  220. public function __construct($model_data = null) {
  221. if(is_null($model_data)) {
  222. $model_data = (object) [];
  223. }
  224. if(is_array($model_data)) {
  225. $model_data = (object) $model_data;
  226. }
  227. $this->model_data = $model_data;
  228. $this->in_db = !is_null($this->getID());
  229. }
  230. // Object methods
  231. public final function isModified() {
  232. return !empty($this->modified_fields);
  233. }
  234. public final function getModifiedFields() {
  235. return $this->modified_fields;
  236. }
  237. private function preCommit() {
  238. }
  239. private final function getIDWhere() {
  240. $id_field = static::$id_field;
  241. $id = $this->getID();
  242. $nvp = [];
  243. if(is_array($id) && is_array($id_field)) {
  244. $nvp = array_combine($id_field, $id);
  245. }
  246. else {
  247. $nvp[$id_field] = $id;
  248. }
  249. $where = db::get_where_string($nvp, true);
  250. return $where;
  251. }
  252. public final function commit($testing = false) {
  253. $this->preCommit();
  254. $id_field = static::$id_field;
  255. $table = static::$table;
  256. $blob_fields = static::$blob_fields;
  257. $nvp = [];
  258. foreach($this->modified_fields as $field) {
  259. if(in_array($field, $blob_fields)) {
  260. $nvp[$field] = "x'" . bin2hex($this->model_data->$field) . "'";
  261. }
  262. // NULL
  263. else if(is_null($this->model_data->$field)) {
  264. $nvp[$field] = null;
  265. }
  266. // Value
  267. else {
  268. $nvp[$field] = db::escape($this->model_data->$field);
  269. }
  270. }
  271. if(empty($nvp)) return true;
  272. $value_string = db::get_value_string($nvp);
  273. if($this->in_db) {
  274. // Update
  275. $where = $this->getIDWhere();
  276. $sql = "UPDATE `$table` SET $value_string WHERE $where";
  277. }
  278. else {
  279. // Insert
  280. $sql = "INSERT INTO `$table` SET $value_string";
  281. }
  282. if($testing == false) {
  283. $result = db::query($sql);
  284. if(empty($this->in_db) && $result) {
  285. // Set ID
  286. $insert_id = db::insert_id();
  287. if(!empty($insert_id)) {
  288. $this->$id_field = $insert_id;
  289. }
  290. $this->in_db = true;
  291. }
  292. }
  293. else {
  294. db::$last_query = $sql;
  295. $result = true;
  296. if(empty($this->in_db) && !is_array(static::$id_field)) {
  297. $this->$id_field = mt_rand(1, 32768);
  298. }
  299. $this->in_db = true;
  300. }
  301. if($result) {
  302. $this->modified_fields = [];
  303. }
  304. return !empty($result);
  305. }
  306. public function testCommit() {
  307. return $this->commit(true);
  308. }
  309. public final function commitOrException($testing = false) {
  310. $success = $this->commit($testing);
  311. if(!$success) throw new Exception("Database error.");
  312. return $success;
  313. }
  314. public function delete() {
  315. $table = static::$table;
  316. $id = $this->getID();
  317. if(is_null($id)) return false;
  318. $where = $this->getIDWhere();
  319. $sql = "DELETE FROM `$table` WHERE $where";
  320. $result = db::query($sql);
  321. return empty($result) ? false : db::affected_rows() ? true : false;
  322. }
  323. public final function deleteOrException() {
  324. $success = $this->delete();
  325. if(!$success) throw new Exception("Database error.");
  326. return $success;
  327. }
  328. public final function getID() {
  329. $id_field = static::$id_field;
  330. if(is_array($id_field)) {
  331. $values = [];
  332. $fields = $id_field;
  333. foreach($fields as $field) {
  334. $value = $this->model_data->$field ?? null;
  335. if(is_null($value)) return null;
  336. $values[] = $this->model_data->$field;
  337. }
  338. return $values;
  339. }
  340. else {
  341. return $this->model_data->$id_field ?? null;
  342. }
  343. }
  344. public final function getModelData() {
  345. return clone $this->model_data;
  346. }
  347. }
  348. ?>