1
0

model.php 9.3 KB

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