Files
MoneyFlix/crm/Core/BaseModel.php
T
1sonhador 97eca96f83 MoneyFlix: Add Auth module, fix error monitoring, improve lock screen functionality
- Add complete Auth module with login, logout, lock screen functionality
- Fix 401 error on error monitoring page by adding /error-monitoring/ to public patterns
- Improve lock screen with proper form handling and unlock functionality
- Add RoleModel and role management views
- Add BaseModel for common model functionality
- Update authentication system and middleware
- Add troubleshooting documentation
- Update assets and build tools
2025-10-22 18:09:43 +01:00

95 lines
2.0 KiB
PHP

<?php
namespace App\Core;
use App\Core\DatabaseManager;
abstract class BaseModel
{
protected DatabaseManager $db;
protected string $table = '';
public function __construct(DatabaseManager $db)
{
$this->db = $db;
}
/**
* Get all records from the table
*/
public function getAll(): array
{
$sql = "SELECT * FROM {$this->table}";
return $this->db->fetchAll($sql) ?: [];
}
/**
* Get a record by ID
*/
public function getById(int $id): ?array
{
$sql = "SELECT * FROM {$this->table} WHERE id = ?";
$result = $this->db->fetch($sql, [$id]);
return $result ?: null;
}
/**
* Find a record by ID (alias for getById)
*/
public function find(int $id): ?array
{
return $this->getById($id);
}
/**
* Create a new record
*/
public function create(array $data): bool
{
$columns = array_keys($data);
$placeholders = array_fill(0, count($columns), '?');
$sql = sprintf(
"INSERT INTO %s (%s) VALUES (%s)",
$this->table,
implode(', ', $columns),
implode(', ', $placeholders)
);
return $this->db->query($sql, array_values($data)) > 0;
}
/**
* Update a record by ID
*/
public function update(int $id, array $data): bool
{
$sets = [];
foreach (array_keys($data) as $column) {
$sets[] = "{$column} = ?";
}
$sql = sprintf(
"UPDATE %s SET %s WHERE id = ?",
$this->table,
implode(', ', $sets)
);
$params = array_values($data);
$params[] = $id;
return $this->db->query($sql, $params) > 0;
}
/**
* Delete a record by ID
*/
public function delete(int $id): bool
{
$sql = "DELETE FROM {$this->table} WHERE id = ?";
return $this->db->query($sql, [$id]) > 0;
}
}