diff --git a/AroMVC/Database/Insertable.php b/AroMVC/Database/Insertable.php index 3e81634..1e0ef0b 100644 --- a/AroMVC/Database/Insertable.php +++ b/AroMVC/Database/Insertable.php @@ -3,20 +3,37 @@ namespace AroMVC\Database; class Insertable extends Queryable { protected $table = null; - protected $fields = []; public function __construct(string $table) { $this->table = $table; } public function value(string $name, $value): Insertable { - $fields[$name] = $value; + $this->params[$name] = $value; return $this; } public function values(array $values): Insertable { - foreach($values as $name => $value) - $fields[$name] = $value; + $this->params = array_merge($this->params, $values); + return $this; + } + + public function clear(): Insertable { + $this->params = []; + return $this; + } + + public function execute() { + $fields = "(". implode(", ", array_map(function($val) { + return "`$val`"; + }, array_keys($this->params))) .")"; + + $values = "(". implode(", ", array_map(function($val) { + return ":$val"; + }, array_values($this->params))) .")"; + + $this->query = implode(" ", ["INSERT INTO `$this->table`", $fields, "VALUES", $values]); + parent::execute(); return $this; } } \ No newline at end of file diff --git a/AroMVC/Database/Modifiable.php b/AroMVC/Database/Modifiable.php index 8f57e49..53bafa6 100644 --- a/AroMVC/Database/Modifiable.php +++ b/AroMVC/Database/Modifiable.php @@ -2,5 +2,5 @@ namespace AroMVC\Database; class Modifiable extends Queryable { - + } \ No newline at end of file