$name ?? null; } if(isset(static::$field_map[$name])) { $name = static::$field_map[$name]; } return isset($this->model_data->$name) ? $this->model_data->$name : null; } public function __set($name, $value) { if(in_array($name, static::$object_fields)) { $this->$name = $value; return; } if(isset(static::$field_map[$name])) { $name = static::$field_map[$name]; } $id_field = static::$id_field; if($name == "model_data") { $this->model_data = $value; } else if(in_array($name, static::$fillable_fields)) { if(!is_null($value)) { $value = strval($value); } if(!isset($this->model_data->$name) || ($this->model_data->$name !== $value)) { $this->model_data->$name = $value; $this->modified_fields[] = $name; } } else if($name == $id_field && empty($this->model_data->$id_field)) { $this->model_data->$id_field = $value; } else if(static::$enforce_field_check) { throw new Exception("Attempt to set an unfillable field $name"); } else { $this->model_data->$name = $value; } } public function __isset($name) { $value = $this->$name; return !is_null($value); } // Static method public static function get($id) { $table = static::$table; $is_compound_key = is_array(static::$id_field); // Check repo for item if(!$is_compound_key && isset(static::$repositories[$table][$id])) { return static::$repositories[$table][$id]; } $id_field = static::$id_field; $class = get_called_class(); if($is_compound_key) { // Compound key $args = func_get_args(); $fields = $id_field; if(count($args) != count($fields)) return false; foreach($fields as $index => $field) { $nvp[$field] = $args[$index]; } } else { $nvp[$id_field] = $id; } $where = db::get_where_string($nvp, true); $sql = "SELECT * FROM `$table` WHERE $where"; $result = db::query($sql); $object = db::object_or_false($result); if($object) { $object = new $class($object); if(!$is_compound_key) { static::$repositories[$table][$id] = $object; } } return $object; } public static function getOrException($id, $exception_message = false) { if(!$exception_message) $exception_message = get_called_class() . " not found."; $object = static::get($id); if(!$object) throw new Exception($exception_message); return $object; } public static function getAll() { $objects = []; $table = static::$table; $sql = "SELECT * FROM `$table`"; $result = db::query($sql); while($row = db::fetch_object($result)) { $objects[] = new static($row); } return $objects; } public static function load(...$id_sets) { $table = static::$table; $ids = []; foreach($id_sets as $set) { $ids = array_merge($ids, $set); } if(empty($ids)) return []; $ids = array_unique($ids); $items = static::query([static::$id_field => $ids], ["id_as_key" => true]); foreach($items as $id => $item) { static::$repositories[$table][$id] = $item; } return $items; } public static function loadAll() { $table = static::$table; $items = static::query([], ["id_as_key" => true]); foreach($items as $id => $item) { static::$repositories[$table][$id] = $item; } return $items; } public static function deleteMultiple($nvp = []) { $table = static::$table; $vs = "1"; if(!empty($nvp)) { $nvp = static::getRemappedNVP($nvp); $vs = db::get_where_string($nvp); } $objects = []; $sql = "DELETE FROM `$table` WHERE $vs"; $result = db::query($sql); if(!$result) return false; return db::affected_rows(); } public static function query($nvp = [], $options = []) { if(is_array($nvp)) { return static::findByNVP($nvp, $options); } else { return static::findByValueString($nvp, $options); } } public static function queryFuzzy($nvp = [], $options = []) { $options["fuzzy"] = true; return static::findByNVP($nvp, $options); } public static function queryOne($nvp = [], $options = []) { $rows = static::findByNVP($nvp, $options); return $rows ? $rows[0] : false; } private static function getRemappedNVP($nvp = []) { $mapped_nvp = []; foreach($nvp as $field => $value) { $field = static::$field_map[$field] ?? $field; $mapped_nvp[$field] = $value; } return $mapped_nvp; } public static function count($nvp = []) { $table = static::$table; $vs = "1"; if(!empty($nvp)) { $nvp = static::getRemappedNVP($nvp); $vs = db::get_where_string($nvp); } $objects = []; $sql = "SELECT COUNT(*) AS `count` FROM `$table` WHERE $vs"; $result = db::query($sql); $row = db::fetch_object($result); return $row->count; } public static function findByNVP($nvp, $options = []) { if(empty($nvp)) { $vs = "1"; } else { $nvp = static::getRemappedNVP($nvp); $vs = db::get_where_string($nvp, $options); } return static::findByValueString($vs, $options); } private static function findByValueString($vs, $options = []) { $table = static::$table; $options = (object) $options; $objects = []; $sql = "SELECT * FROM `$table` WHERE $vs"; if(isset($options->sort)) { $sort = static::$field_map[$options->sort] ?? $options->sort; $sql .= " ORDER BY `$sort`"; if(isset($options->order)) { $sql .= " " . strtoupper($options->order); } } if(isset($options->limit)) { $sql .= " LIMIT"; if(isset($options->offset)) { $sql .= " $options->offset,"; } $sql .= " $options->limit"; } $result = db::query($sql); while($row = db::fetch_object($result)) { $objects[] = new static($row); } if(!empty($options->id_as_key)) { $objects = Util::arrayFlipByID($objects); } return $objects; } // Constructor public function __construct($model_data = null) { if(is_null($model_data)) { $model_data = (object) []; } if(is_array($model_data)) { $model_data = (object) $model_data; } $this->model_data = $model_data; $this->in_db = !is_null($this->getID()); } // Object methods public final function isModified() { return !empty($this->modified_fields); } public final function getModifiedFields() { return $this->modified_fields; } private function preCommit() { } private final function getIDWhere() { $id_field = static::$id_field; $id = $this->getID(); $nvp = []; if(is_array($id)) { $nvp = array_combine($id_field, $id); } else { $nvp[$id_field] = $id; } $where = db::get_where_string($nvp, true); return $where; } public final function commit($testing = false) { $this->preCommit(); $id_field = static::$id_field; $table = static::$table; $blob_fields = static::$blob_fields; $nvp = []; foreach($this->modified_fields as $field) { if(in_array($field, $blob_fields)) { $nvp[$field] = "x'" . bin2hex($this->model_data->$field) . "'"; } // NULL else if(is_null($this->model_data->$field)) { $nvp[$field] = null; } // Value else { $nvp[$field] = db::escape($this->model_data->$field); } } if(empty($nvp)) return true; $value_string = db::get_value_string($nvp); if($this->in_db) { // Update $where = $this->getIDWhere(); $sql = "UPDATE `$table` SET $value_string WHERE $where"; } else { // Insert $sql = "INSERT INTO `$table` SET $value_string"; } if($testing == false) { $result = db::query($sql); if(empty($this->in_db) && $result) { // Set ID $insert_id = db::insert_id(); if(!empty($insert_id)) { $this->$id_field = $insert_id; } $this->in_db = true; } } else { db::$last_query = $sql; $result = true; if(empty($this->in_db) && !is_array(static::$id_field)) { $this->$id_field = mt_rand(1, 32768); } $this->in_db = true; } return !empty($result); } public function testCommit() { return $this->commit(true); } public final function commitOrException($testing = false) { $success = $this->commit($testing); if(!$success) throw new Exception("Database error."); return $success; } public function delete() { $table = static::$table; $id = $this->getID(); if(is_null($id)) return false; $where = $this->getIDWhere(); $sql = "DELETE FROM `$table` WHERE $where"; $result = db::query($sql); return empty($result) ? false : db::affected_rows() ? true : false; } public final function deleteOrException() { $success = $this->delete(); if(!$success) throw new Exception("Database error."); return $success; } public final function getID() { $id_field = static::$id_field; if(is_array($id_field)) { $values = []; $fields = $id_field; foreach($fields as $field) { $value = $this->model_data->$field ?? null; if(is_null($value)) return null; $values[] = $this->model_data->$field; } return $values; } else { return $this->model_data->$id_field ?? null; } } public final function getModelData() { return clone $this->model_data; } } ?>