From d536a8f4c4b8da91e8c6172cdf2ed057fa38e262 Mon Sep 17 00:00:00 2001 From: Malloc of Kuzkycyziklistan Date: Thu, 2 Mar 2017 16:02:38 -0600 Subject: [PATCH] another wow mewoio --- AroMVC/Base/Controller.php | 2 +- AroMVC/Base/Model.php | 25 ++++++++++++-------- AroMVC/Base/ViewModel.php | 6 +++++ AroMVC/Database/Database.php | 2 +- AroMVC/Database/Queryable.php | 3 ++- AroMVC/Database/Selectable.php | 14 ++++++++---- Controllers/Home.php | 1 + Models/Company.php | 12 ++++------ Models/State.php | 7 +++--- index.php | 42 ++++++++++++++++++++-------------- 10 files changed, 69 insertions(+), 45 deletions(-) create mode 100644 AroMVC/Base/ViewModel.php diff --git a/AroMVC/Base/Controller.php b/AroMVC/Base/Controller.php index f6af385..a650fe4 100644 --- a/AroMVC/Base/Controller.php +++ b/AroMVC/Base/Controller.php @@ -1,6 +1,6 @@ initialize(); + $row = db::select("*") ->from(self::getTable()) + ->where("`". self::$index ."` = :id") + ->params(["id" => $id]) + ->asArray(); - return $new; + if(count($row) == 0) + throw new \Exception("Cannot instantiate new ". get_class(new static) ." with id '$id' because it does not exist"); + + return self::withRow($row[0]); } public static function withRow(array $row) { $new = new static(); @@ -30,7 +36,8 @@ abstract class Model { protected abstract function initialize(); protected static function getTable(): string { - return self::$table ?? get_class(new static); + $fqcn = get_class(new static); + return self::$table ?? substr($fqcn, strrpos($fqcn, "\\")+1); } protected function get(string $name) { @@ -72,15 +79,15 @@ abstract class Model { $this->associations[strtolower($associationName)] = strtolower($rawName); } - public static function select(): Selectable { - return db::select("*", new static)->from(self::getTable()); + public static function select($columns = "*"): Selectable { + return db::select($columns, new static)->from(self::getTable()); } - public function update() { + public function update(): void { if(count($this->modified) == 0) return; - var $query = db::update(self::getTable()); + $query = db::update(self::getTable()); foreach($this->modified as $field) // TODO set ... value diff --git a/AroMVC/Base/ViewModel.php b/AroMVC/Base/ViewModel.php new file mode 100644 index 0000000..7fa7691 --- /dev/null +++ b/AroMVC/Base/ViewModel.php @@ -0,0 +1,6 @@ +results = db::rawRowQuery($this->query, $this->params); + return $this; } public function getRawQuery() { diff --git a/AroMVC/Database/Selectable.php b/AroMVC/Database/Selectable.php index 3b94599..291e4a0 100644 --- a/AroMVC/Database/Selectable.php +++ b/AroMVC/Database/Selectable.php @@ -28,7 +28,7 @@ class Selectable extends Queryable { protected function getModelReflection($model): \ReflectionClass { $type = new \ReflectionClass($model); - if(!$type->isSubclassOf("\\AroMVC\\Core\\AroModel")) + if(!$type->isSubclassOf("\\AroMVC\\Base\\Model")) throw new \Exception("Cannot instantiate non-model object."); return $type; @@ -73,7 +73,7 @@ class Selectable extends Queryable { if(!$this->isEmpty(AMVC_QRY_SEL_WHERE)) { $wheres = array_map(function($x) { - return "($x)"; + return "$x"; }, $this->components[AMVC_QRY_SEL_WHERE]); array_push($query, "WHERE", implode(" AND ", $wheres)); } @@ -191,19 +191,23 @@ class Selectable extends Queryable { public function asArray() { if($this->results == null) - throw new \Exception("Cannot return results from a query that has not executed."); + $this->execute(); return $this->results; } public function asModels($model = null) { + echo "hi"; + if($this->results == null) - throw new \Exception("Cannot return results from a query that has not executed."); + $this->execute(); if($this->model == null || $model != null) $this->model = $this->getModelReflection($model); - foreach($this->results as $result) + var_dump($this->results); + foreach($this->results as $result) { yield $this->model->getMethod("withRow")->invoke(null, $result); + } } } \ No newline at end of file diff --git a/Controllers/Home.php b/Controllers/Home.php index a86b135..fe0abb2 100644 --- a/Controllers/Home.php +++ b/Controllers/Home.php @@ -1,4 +1,5 @@ addHook("states", function() { - return db::select("*") - ->from("States") - ->where("`cid` = ?") - ->params([$this->get("id")]); + return State::select() ->where("`cid` = :cid") + ->params(["cid" => $this->get("id")]); }); } } \ No newline at end of file diff --git a/Models/State.php b/Models/State.php index 5df8be7..4dd63ea 100644 --- a/Models/State.php +++ b/Models/State.php @@ -1,10 +1,9 @@ value("namespace"); + $userns = conf::section("Project")->value("namespace"); $class = str_replace("_", "\\", $class); $class = ltrim($class, '\\'); $parts = explode("\\", $class); if($parts[0] == "AroMVC") { - if(count($parts) < 2) + if(count($parts) < 3) die("Autoloader failed: malformed class name $class"); if($parts[1] == "Core") - require_once "AroMVC". DIRECTORY_SEPARATOR . $parts[2] .".php"; - else - die("Autoloader failed: malformed class name $class"); + return; + + require_once "AroMVC" . DIRECTORY_SEPARATOR . $parts[1] . DIRECTORY_SEPARATOR . $parts[2] . ".php"; } else if($parts[0] == $userns) { if(in_array($parts[1], ["Controllers", "Models", "ViewModels"])) - require_once $parts[1]. DIRECTORY_SEPARATOR. $parts[2] .".php"; + require_once $parts[1] . DIRECTORY_SEPARATOR . $parts[2] .".php"; } }); +db::initialize(); // TODO write error handler -$tmp = new Selectable("*"); +$company = Company::withId(1); -$tmp->from("Companies") - ->where("`name` = :name OR `id` = :cid") - ->params(["name" => "winco", "cid" => 20]) - ->join("LEFT JOIN", "Invoices") - ->using("id") - ->execute() - ->asModels(new Company); \ No newline at end of file +var_dump( + /*$tmp->from("Companies") + ->where("`name` = :name OR `id` = :cid") + ->params(["name" => "winco", "cid" => 20]) + ->join("LEFT JOIN", "Invoices") + ->using("id") + ->execute() + ->asModels(new Company)*/ + $company +); + +echo $company->name; + +var_dump($company->states->asModels()); \ No newline at end of file