Add PHP Media API scaffold and Docker configs
Initial project scaffold for a PHP Media API including routing, controllers, models and services under api/ (Router, Media/Cast/Image/Settings controllers, models, database/bootstrap files and automatic docs service). Adds Docker support (Dockerfile, docker-compose.yml, DOCKER_README.md, php-custom.ini), .htaccess for pretty URLs, API documentation and example payloads (API_EXAMPLES.md, api/README.md, api_examples/*.json), image handling service and logging, plus a comprehensive .gitignore. This commit provides a runnable development environment and example requests to get the API up and tested quickly.
This commit is contained in:
100
api/models/BaseModel.php
Normal file
100
api/models/BaseModel.php
Normal file
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
abstract class BaseModel {
|
||||
protected $pdo;
|
||||
protected $table;
|
||||
|
||||
public function __construct($pdo) {
|
||||
$this->pdo = $pdo;
|
||||
}
|
||||
|
||||
protected function findById($id) {
|
||||
$stmt = $this->pdo->prepare("SELECT * FROM {$this->table} WHERE id = ?");
|
||||
$stmt->execute([$id]);
|
||||
return $stmt->fetch();
|
||||
}
|
||||
|
||||
protected function findAll($conditions = [], $orderBy = 'createdAt DESC', $limit = null, $offset = null) {
|
||||
$query = "SELECT * FROM {$this->table} WHERE 1=1";
|
||||
$params = [];
|
||||
|
||||
foreach ($conditions as $field => $value) {
|
||||
if (is_array($value)) {
|
||||
// LIKE Operator
|
||||
$query .= " AND $field LIKE ?";
|
||||
$params[] = $value[0];
|
||||
} else {
|
||||
$query .= " AND $field = ?";
|
||||
$params[] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
$query .= " ORDER BY $orderBy";
|
||||
|
||||
if ($limit) {
|
||||
$query .= " LIMIT " . (int)$limit;
|
||||
}
|
||||
|
||||
if ($offset) {
|
||||
$query .= " OFFSET " . (int)$offset;
|
||||
}
|
||||
|
||||
$stmt = $this->pdo->prepare($query);
|
||||
$stmt->execute($params);
|
||||
return $stmt->fetchAll();
|
||||
}
|
||||
|
||||
protected function count($conditions = []) {
|
||||
$query = "SELECT COUNT(*) FROM {$this->table} WHERE 1=1";
|
||||
$params = [];
|
||||
|
||||
foreach ($conditions as $field => $value) {
|
||||
if (is_array($value)) {
|
||||
$query .= " AND $field LIKE ?";
|
||||
$params[] = $value[0];
|
||||
} else {
|
||||
$query .= " AND $field = ?";
|
||||
$params[] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
$stmt = $this->pdo->prepare($query);
|
||||
$stmt->execute($params);
|
||||
return $stmt->fetchColumn();
|
||||
}
|
||||
|
||||
protected function create($data) {
|
||||
$fields = array_keys($data);
|
||||
$placeholders = array_fill(0, count($fields), '?');
|
||||
|
||||
$query = "INSERT INTO {$this->table} (" . implode(', ', $fields) . ") VALUES (" . implode(', ', $placeholders) . ")";
|
||||
$stmt = $this->pdo->prepare($query);
|
||||
$stmt->execute(array_values($data));
|
||||
|
||||
return $this->pdo->lastInsertId();
|
||||
}
|
||||
|
||||
protected function update($id, $data) {
|
||||
$fields = [];
|
||||
$params = [];
|
||||
|
||||
foreach ($data as $field => $value) {
|
||||
$fields[] = "$field = ?";
|
||||
$params[] = $value;
|
||||
}
|
||||
|
||||
$params[] = $id;
|
||||
|
||||
$query = "UPDATE {$this->table} SET " . implode(', ', $fields) . " WHERE id = ?";
|
||||
$stmt = $this->pdo->prepare($query);
|
||||
$stmt->execute($params);
|
||||
|
||||
return $stmt->rowCount() > 0;
|
||||
}
|
||||
|
||||
protected function delete($id) {
|
||||
$stmt = $this->pdo->prepare("DELETE FROM {$this->table} WHERE id = ?");
|
||||
$stmt->execute([$id]);
|
||||
return $stmt->rowCount() > 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user