woo
my
This commit is contained in:
parent
0b8cbe457c
commit
f47764025c
3 changed files with 53 additions and 3 deletions
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
|
|
Reference in a new issue