66 lines
1.7 KiB
PHP
66 lines
1.7 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace Core;
|
||
|
|
|
||
|
|
use PDO;
|
||
|
|
|
||
|
|
abstract class Model
|
||
|
|
{
|
||
|
|
protected static string $table;
|
||
|
|
protected PDO $db;
|
||
|
|
|
||
|
|
public function __construct()
|
||
|
|
{
|
||
|
|
$this->db = Database::getConnection();
|
||
|
|
}
|
||
|
|
|
||
|
|
public function all(): array
|
||
|
|
{
|
||
|
|
$stmt = $this->db->query("SELECT * FROM " . static::$table . " ORDER BY id DESC");
|
||
|
|
return $stmt->fetchAll();
|
||
|
|
}
|
||
|
|
|
||
|
|
public function find(int $id): ?array
|
||
|
|
{
|
||
|
|
$stmt = $this->db->prepare("SELECT * FROM " . static::$table . " WHERE id = ?");
|
||
|
|
$stmt->execute([$id]);
|
||
|
|
$result = $stmt->fetch();
|
||
|
|
return $result ?: null;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function create(array $data): int
|
||
|
|
{
|
||
|
|
$columns = implode(', ', array_keys($data));
|
||
|
|
$placeholders = implode(', ', array_fill(0, count($data), '?'));
|
||
|
|
|
||
|
|
$sql = "INSERT INTO " . static::$table . " ($columns) VALUES ($placeholders)";
|
||
|
|
$stmt = $this->db->prepare($sql);
|
||
|
|
$stmt->execute(array_values($data));
|
||
|
|
|
||
|
|
return (int)$this->db->lastInsertId();
|
||
|
|
}
|
||
|
|
|
||
|
|
public function update(int $id, array $data): bool
|
||
|
|
{
|
||
|
|
$set = implode(', ', array_map(fn($col) => "$col = ?", array_keys($data)));
|
||
|
|
$sql = "UPDATE " . static::$table . " SET $set WHERE id = ?";
|
||
|
|
|
||
|
|
$stmt = $this->db->prepare($sql);
|
||
|
|
return $stmt->execute([...array_values($data), $id]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function delete(int $id): bool
|
||
|
|
{
|
||
|
|
$stmt = $this->db->prepare("DELETE FROM " . static::$table . " WHERE id = ?");
|
||
|
|
return $stmt->execute([$id]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function where(string $column, $value): array
|
||
|
|
{
|
||
|
|
$stmt = $this->db->prepare("SELECT * FROM " . static::$table . " WHERE $column = ?");
|
||
|
|
$stmt->execute([$value]);
|
||
|
|
return $stmt->fetchAll();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|