diff --git a/AroMVC/Database/Insertable.php b/AroMVC/Database/Insertable.php index 1e0ef0b..d1fbff8 100644 --- a/AroMVC/Database/Insertable.php +++ b/AroMVC/Database/Insertable.php @@ -30,9 +30,9 @@ class Insertable extends Queryable { $values = "(". implode(", ", array_map(function($val) { return ":$val"; - }, array_values($this->params))) .")"; + }, array_keys($this->params))) .")"; - $this->query = implode(" ", ["INSERT INTO `$this->table`", $fields, "VALUES", $values]); + $this->query = implode(" ", ["INSERT INTO", "`$this->table`", $fields, "VALUES", $values]); parent::execute(); return $this; } diff --git a/AroMVC/Database/Modifiable.php b/AroMVC/Database/Modifiable.php index 53bafa6..262edf1 100644 --- a/AroMVC/Database/Modifiable.php +++ b/AroMVC/Database/Modifiable.php @@ -2,5 +2,51 @@ namespace AroMVC\Database; class Modifiable extends Queryable { - + protected $table = null; + protected $wheres = []; + + public function __construct(string $table) { + $this->table = $table; + } + + public function set(string $name, $value): Modifiable { + $this->params[$name] = $value; + return $this; + } + + public function setMany(array $values): Modifiable { + $this->params = array_merge($this->params, $values); + return $this; + } + + public function clear(): Modifiable { + $this->params = []; + return $this; + } + + public function onId(int $id, string $idField = "id"): Modifiable { + $this->wheres[] = "`$idField` = :AmvcIdXyz"; + $this->param("AmvcIdXyz", $id); + return $this; + } + + public function where(string $condition) : Modifiable { + $this->wheres[] = $condition; + return $this; + } + + public function execute() { + $sets = array_map(function($key) { + return "`$key` = :$key"; + }, array_keys($this->params)); + + $this->query = implode(" ", ["UPDATE", "`$this->table`", "SET", implode(", ", $sets)]); + if(count($this->wheres) > 0) { + $this->query .= " WHERE " . implode(" AND ", array_map(function($val) { + return "($val)"; + }, $this->wheres)); + } + parent::execute(); + return $this; + } } \ No newline at end of file diff --git a/AroMVC/Database/Queryable.php b/AroMVC/Database/Queryable.php index 01777d8..caba678 100644 --- a/AroMVC/Database/Queryable.php +++ b/AroMVC/Database/Queryable.php @@ -9,6 +9,10 @@ abstract class Queryable { protected $flags = 0; protected $locks = 0; + public function param(string $name, $value) { + $this->params[$name] = $value; + } + public function params(array $params) { $this->params = array_merge($this->params, $params); return $this;