diff --git a/AroMVC/Base/Controller.php b/AroMVC/Base/Controller.php new file mode 100644 index 0000000..f6af385 --- /dev/null +++ b/AroMVC/Base/Controller.php @@ -0,0 +1,6 @@ +initialize(); return $new; @@ -29,8 +29,8 @@ abstract class Model { } protected abstract function initialize(); - protected function setTable(string $tableName) { - $this->table = $tableName; + protected static function getTable(): string { + return self::$table ?? get_class(new static); } protected function get(string $name) { @@ -53,11 +53,15 @@ abstract class Model { $name = strtolower($name); if(array_key_exists($name, $this->associations)) - $rawData[$this->associations[$name]] = $value; + $modified = $this->associations[$name]; else if(array_key_exists($name, $this->rawData)) - $rawData[$name] = $value; + $modified = $name; else throw new \Exception("Cannot set the value for property '$name'."); + + $rawData[$modified] = $value; + if(!in_array($modified, $this->modified)) + $this->modified[] = $modified; } protected function addHook(string $name, $func) { @@ -69,14 +73,22 @@ abstract class Model { } public static function select(): Selectable { - return db::select("*", new static)->from(self::$table); + return db::select("*", new static)->from(self::getTable()); } public function update() { + if(count($this->modified) == 0) + return; + var $query = db::update(self::getTable()); + foreach($this->modified as $field) + // TODO set ... value + + db::batchQuery($query); + $this->modified = []; } - public function delete() { + public function delete(): void { } } \ No newline at end of file diff --git a/AroMVC/ConfigSection.php b/AroMVC/Configuration/ConfigSection.php similarity index 95% rename from AroMVC/ConfigSection.php rename to AroMVC/Configuration/ConfigSection.php index 13af851..a53f473 100644 --- a/AroMVC/ConfigSection.php +++ b/AroMVC/Configuration/ConfigSection.php @@ -1,5 +1,5 @@ value("namespace"); + $class = str_replace("_", "\\", $class); $class = ltrim($class, '\\'); $parts = explode("\\", $class); if($parts[0] == "AroMVC") { - if(count($parts) < 3) + if(count($parts) < 2) die("Autoloader failed: malformed class name $class"); if($parts[1] == "Core") require_once "AroMVC". DIRECTORY_SEPARATOR . $parts[2] .".php"; - else if(in_array($parts[1], ["Controllers", "Models", "ViewModels"])) - require_once $parts[1]. DIRECTORY_SEPARATOR. $parts[2] .".php"; else die("Autoloader failed: malformed class name $class"); + } else if($parts[0] == $userns) { + if(in_array($parts[1], ["Controllers", "Models", "ViewModels"])) + require_once $parts[1]. DIRECTORY_SEPARATOR. $parts[2] .".php"; } }); // TODO write error handler -Configuration::initialize(file_get_contents("conf.ini")); -Database::initialize(); - $tmp = new Selectable("*"); $tmp->from("Companies")