diff --git a/web/system/engine/hf_core.php b/web/system/engine/hf_core.php index bb42246..752b7e6 100644 --- a/web/system/engine/hf_core.php +++ b/web/system/engine/hf_core.php @@ -307,8 +307,9 @@ class HF_Core public function runMigrations() { global $argv; $this->setupDatabaseConnection(); + $autoIncrement = \system\engine\HF_Model::AUTOINCREMENT_SQLITE; DB::query("CREATE TABLE IF NOT EXISTS migrations ( - id INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT, + id INTEGER PRIMARY KEY $autoIncrement, migration INTEGER, ran_at DATETIME )"); diff --git a/web/system/engine/hf_model.php b/web/system/engine/hf_model.php index 58e7634..9ecb5b4 100644 --- a/web/system/engine/hf_model.php +++ b/web/system/engine/hf_model.php @@ -7,6 +7,10 @@ use \vendor\DB\DB; abstract class HF_Model { public $id = null; + + const AUTOINCREMENT_SQLITE = "AUTOINCREMENT"; + const AUTOINCREMENT_MYSQL = "AUTO_INCREMENT"; + public static function create($data) { $obj = new static(); @@ -29,7 +33,7 @@ abstract class HF_Model { $fieldMap[$column] = $this->$column; } if ($fieldMap["id"] == null) { - DB::insert($table, $fieldMap); + $this->id = DB::insert($table, $fieldMap); } else { $updateFields = $fieldMap; unset($updateFields["id"]); @@ -74,4 +78,11 @@ abstract class HF_Model { return DB::fetchObject("SELECT $fields FROM $table WHERE $field = ?", get_called_class(), [$value]); } + public static function all() { + $function = new \ReflectionClass(get_called_class()); + $table = strtolower($function->getShortName()); + $fields = implode(", ", DB::getColumns($table)); + return DB::fetchObject("SELECT $fields FROM $table", get_called_class(), []); + } + } \ No newline at end of file