my
This commit is contained in:
Malloc of Kuzkycyziklistan 2017-03-09 15:59:53 -06:00
parent 0b8cbe457c
commit f47764025c
3 changed files with 53 additions and 3 deletions

View file

@ -30,9 +30,9 @@ class Insertable extends Queryable {
$values = "(". implode(", ", array_map(function($val) { $values = "(". implode(", ", array_map(function($val) {
return ":$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(); parent::execute();
return $this; return $this;
} }

View file

@ -2,5 +2,51 @@
namespace AroMVC\Database; namespace AroMVC\Database;
class Modifiable extends Queryable { 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;
}
} }

View file

@ -9,6 +9,10 @@ abstract class Queryable {
protected $flags = 0; protected $flags = 0;
protected $locks = 0; protected $locks = 0;
public function param(string $name, $value) {
$this->params[$name] = $value;
}
public function params(array $params) { public function params(array $params) {
$this->params = array_merge($this->params, $params); $this->params = array_merge($this->params, $params);
return $this; return $this;