another wow
mewoio
This commit is contained in:
parent
f642090d47
commit
d536a8f4c4
10 changed files with 69 additions and 45 deletions
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
namespace AroMVC\Base;
|
||||
|
||||
class Controller {
|
||||
abstract class Controller {
|
||||
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
<?php
|
||||
namespace AroMVC\Base;
|
||||
use AroMVC\Database\Database as db;
|
||||
use AroMVC\Database\Selectable;
|
||||
|
||||
abstract class Model {
|
||||
protected $rawData = [];
|
||||
|
@ -8,16 +9,21 @@ abstract class Model {
|
|||
protected $hooks = [];
|
||||
|
||||
protected static $table = null;
|
||||
protected $index = "id";
|
||||
protected static $index = "id";
|
||||
protected $deleted = false;
|
||||
protected $modified = [];
|
||||
|
||||
public function __construct() {}
|
||||
public static function withId(int $id) {
|
||||
$new = new static;
|
||||
$new->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
|
||||
|
||||
|
|
6
AroMVC/Base/ViewModel.php
Normal file
6
AroMVC/Base/ViewModel.php
Normal file
|
@ -0,0 +1,6 @@
|
|||
<?php
|
||||
namespace AroMVC\Base;
|
||||
|
||||
abstract class ViewModel {
|
||||
|
||||
}
|
|
@ -88,7 +88,7 @@ class Database {
|
|||
$query = trim($query);
|
||||
while(($pos = strpos($query, ":")) !== false) {
|
||||
for($end = $pos+1;;$end++) {
|
||||
if(!ctype_alnum($query[$end]))
|
||||
if($end == strlen($query) || !ctype_alnum($query[$end]))
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
namespace AroMVC\Database;
|
||||
use AroMVC\Core\Database as db;
|
||||
use AroMVC\Database\Database as db;
|
||||
|
||||
abstract class Queryable {
|
||||
protected $query = "";
|
||||
|
@ -16,6 +16,7 @@ abstract class Queryable {
|
|||
|
||||
public function execute() {
|
||||
$this->results = db::rawRowQuery($this->query, $this->params);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getRawQuery() {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,4 +1,5 @@
|
|||
<?php
|
||||
namespace Fire\Controllers;
|
||||
|
||||
class Home {
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
namespace AroMVC\Models;
|
||||
use \AroMVC\Core\Model;
|
||||
use \AroMVC\Core\Database as db;
|
||||
namespace Fire\Models;
|
||||
use \AroMVC\Base\Model;
|
||||
use \AroMVC\Database\Database as db;
|
||||
|
||||
class Company extends Model {
|
||||
protected $id;
|
||||
|
@ -10,10 +10,8 @@ class Company extends Model {
|
|||
|
||||
protected function initialize() {
|
||||
$this->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")]);
|
||||
});
|
||||
}
|
||||
}
|
|
@ -1,10 +1,9 @@
|
|||
<?php
|
||||
namespace AroMVC\Models;
|
||||
use \AroMVC\Core\Model;
|
||||
namespace Fire\Models;
|
||||
use \AroMVC\Base\Model;
|
||||
use \AroMVC\Database\Database as db;
|
||||
|
||||
class State extends Model {
|
||||
protected static $table = "State";
|
||||
|
||||
protected $id;
|
||||
protected $name;
|
||||
|
||||
|
|
32
index.php
32
index.php
|
@ -1,42 +1,50 @@
|
|||
<?php
|
||||
namespace AroMVC;
|
||||
include "AroMVC/Configuration/Configuration.php";
|
||||
include "AroMVC/Configuration/ConfigSection.php";
|
||||
use AroMVC\Configuration\Configuration as conf;
|
||||
use AroMVC\Database\Database as db;
|
||||
use AroMVC\Database\Selectable;
|
||||
use AroMVC\Models\Company;
|
||||
use Fire\Models\Company;
|
||||
|
||||
conf::initialize(file_get_contents("conf.ini"));
|
||||
db::initialize();
|
||||
|
||||
spl_autoload_register(function($class) {
|
||||
$userns = Configuration::section("Project")->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")
|
||||
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);
|
||||
->asModels(new Company)*/
|
||||
$company
|
||||
);
|
||||
|
||||
echo $company->name;
|
||||
|
||||
var_dump($company->states->asModels());
|
Reference in a new issue