function dispense($t)
{
$stmt = $this->query('DESCRIBE ' . $t)->fetchAll();
$r = (object)[];
foreach ($stmt as $f) {
foreach ($f as $k => $v) {
if ($k === 'Field') $r->$v = null;
}
}
$r->tablename = $t;
return $r;
}
function insert($o)
{
$t = $o->tablename;
unset($o->tablename);
$keys = [];
$values = [];
if (empty($o->id)) {
foreach ($o as $k => $v) {
$keys[] = $k;
if (empty($v)): $values[] = 'null';
else: $values[] = is_string($v) ? '"' . $v . '"' : $v; endif;
}
$this->query('INSERT INTO ' . $t . ' (' . implode(',', $keys) . ') VALUES (' . implode(',', $values) . ')');
} else {
$id = $o->id;
unset($o->id);
$q = [];
foreach ($o as $k => $v) {
if (is_string($v)) $v = '"' . $v . '"';
$q[] = $k . '=' . $v;
}
$this->query('UPDATE ' . $t . ' SET ' . implode(',', $q) . ' WHERE id = ' . $id);
}
}
function delete($o)
{
$t = $o->tablename;
$this->query('DELETE FROM ' . $t . ' WHERE id = ' . $o->id);
}
У каждой таблицы есть первичный ключ id с auto_increment