51 lines
1.4 KiB
PHP
51 lines
1.4 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace Battles\Models;
|
||
|
|
||
|
use Battles\Database\Db;
|
||
|
|
||
|
class Model
|
||
|
{
|
||
|
protected static string $tableName = '';
|
||
|
protected static string $primaryKey = '';
|
||
|
protected array $columns = [];
|
||
|
|
||
|
protected function getColumnValue($column)
|
||
|
{
|
||
|
return $this->columns[$column];
|
||
|
}
|
||
|
|
||
|
protected function setColumnValue($column, $value)
|
||
|
{
|
||
|
$this->columns[$column] = $value;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Полная перезапись. Отсутствующие параметры превратятся в default|null.
|
||
|
* @return void
|
||
|
*/
|
||
|
protected function save()
|
||
|
{
|
||
|
$q = 'replace into ' . self::$tableName . ' (' . implode(', ', array_keys($this->columns)) . ') values(';
|
||
|
$keys = [];
|
||
|
foreach ($this->columns as $key => $value) {
|
||
|
$keys[':' . $key] = $value;
|
||
|
}
|
||
|
$q .= implode(', ', array_keys($keys)) . ')';
|
||
|
Db::getInstance()->execute($q, $keys);
|
||
|
}
|
||
|
|
||
|
protected function updateByPrimaryKey()
|
||
|
{
|
||
|
$q = 'update ' . self::$tableName . ' set ';
|
||
|
$keys = [];
|
||
|
foreach ($this->columns as $key => $value) {
|
||
|
$keys[':' . $key] = $value;
|
||
|
$q .= $key . ' = :' . $key . ', ';
|
||
|
}
|
||
|
$q = rtrim($q, ',');
|
||
|
$q .= 'where ' . self::$primaryKey . ' = :' . self::$primaryKey;
|
||
|
Db::getInstance()->execute($q, $keys);
|
||
|
}
|
||
|
}
|