mirror of
https://github.com/ceratic/MediaCollectorLibary.git
synced 2026-05-13 23:56:46 +02:00
first commit
This commit is contained in:
108
app/Models/Model.php
Normal file
108
app/Models/Model.php
Normal file
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use PDOException;
|
||||
|
||||
abstract class Model
|
||||
{
|
||||
protected \PDO $pdo;
|
||||
protected string $table;
|
||||
protected array $fillable = [];
|
||||
protected array $hidden = [];
|
||||
protected array $casts = [];
|
||||
|
||||
public function __construct(\PDO $pdo)
|
||||
{
|
||||
$this->pdo = $pdo;
|
||||
}
|
||||
|
||||
public function find(int $id): ?array
|
||||
{
|
||||
$stmt = $this->pdo->prepare("SELECT * FROM {$this->table} WHERE id = :id");
|
||||
$stmt->execute(['id' => $id]);
|
||||
return $stmt->fetch(\PDO::FETCH_ASSOC) ?: null;
|
||||
}
|
||||
|
||||
public function findAll(array $conditions = []): array
|
||||
{
|
||||
$whereClause = $this->buildWhereClause($conditions);
|
||||
$sql = "SELECT * FROM {$this->table} {$whereClause['sql']}";
|
||||
$stmt = $this->pdo->prepare($sql);
|
||||
$stmt->execute($whereClause['params']);
|
||||
return $stmt->fetchAll(\PDO::FETCH_ASSOC);
|
||||
}
|
||||
|
||||
public function create(array $data): ?int
|
||||
{
|
||||
$filteredData = array_intersect_key($data, array_flip($this->fillable));
|
||||
$columns = array_keys($filteredData);
|
||||
$placeholders = array_map(fn($col) => ":$col", $columns);
|
||||
$sql = "INSERT INTO {$this->table} (" . implode(', ', $columns) . ") VALUES (" . implode(', ', $placeholders) . ")";
|
||||
$stmt = $this->pdo->prepare($sql);
|
||||
$stmt->execute($filteredData);
|
||||
return (int) $this->pdo->lastInsertId();
|
||||
}
|
||||
|
||||
public function update(int $id, array $data): bool
|
||||
{
|
||||
$filteredData = array_intersect_key($data, array_flip($this->fillable));
|
||||
if (empty($filteredData)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$setClause = array_map(fn($col) => "$col = :$col", array_keys($filteredData));
|
||||
$sql = "UPDATE {$this->table} SET " . implode(', ', $setClause) . " WHERE id = :id";
|
||||
$filteredData['id'] = $id;
|
||||
|
||||
$stmt = $this->pdo->prepare($sql);
|
||||
return $stmt->execute($filteredData);
|
||||
}
|
||||
|
||||
public function delete(int $id): bool
|
||||
{
|
||||
$stmt = $this->pdo->prepare("DELETE FROM {$this->table} WHERE id = :id");
|
||||
return $stmt->execute(['id' => $id]);
|
||||
}
|
||||
|
||||
public function count(array $conditions = []): int
|
||||
{
|
||||
$whereClause = $this->buildWhereClause($conditions);
|
||||
$sql = "SELECT COUNT(*) FROM {$this->table} {$whereClause['sql']}";
|
||||
$stmt = $this->pdo->prepare($sql);
|
||||
$stmt->execute($whereClause['params']);
|
||||
return (int) $stmt->fetchColumn();
|
||||
}
|
||||
|
||||
protected function buildWhereClause(array $conditions): array
|
||||
{
|
||||
if (empty($conditions)) {
|
||||
return ['sql' => '', 'params' => []];
|
||||
}
|
||||
|
||||
$parts = [];
|
||||
$params = [];
|
||||
|
||||
foreach ($conditions as $column => $value) {
|
||||
if (is_array($value)) {
|
||||
$parts[] = "$column IN (" . implode(', ', array_map(fn($k) => ":$column$k", array_keys($value))) . ")";
|
||||
foreach ($value as $k => $v) {
|
||||
$params["$column$k"] = $v;
|
||||
}
|
||||
} else {
|
||||
$parts[] = "$column = :$column";
|
||||
$params[$column] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
'sql' => 'WHERE ' . implode(' AND ', $parts),
|
||||
'params' => $params
|
||||
];
|
||||
}
|
||||
|
||||
public function getTable(): string
|
||||
{
|
||||
return $this->table;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user