67 lines
1.8 KiB
PHP
67 lines
1.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Core\Database\Database;
|
|
use PDO;
|
|
|
|
class Customer
|
|
{
|
|
public static function all(): array
|
|
{
|
|
$db = Database::getConnection();
|
|
$stmt = $db->query("SELECT * FROM customers ORDER BY name ASC");
|
|
return $stmt->fetchAll();
|
|
}
|
|
|
|
public static function find(int $id): ?array
|
|
{
|
|
$db = Database::getConnection();
|
|
$stmt = $db->prepare("SELECT * FROM customers WHERE id = ?");
|
|
$stmt->execute([$id]);
|
|
$row = $stmt->fetch();
|
|
return $row ?: null;
|
|
}
|
|
|
|
public static function count(): int
|
|
{
|
|
$db = Database::getConnection();
|
|
return (int)$db->query("SELECT COUNT(*) FROM customers")->fetchColumn();
|
|
}
|
|
|
|
public static function create(array $data): int
|
|
{
|
|
$db = Database::getConnection();
|
|
$stmt = $db->prepare("
|
|
INSERT INTO customers (name, email, phone, address)
|
|
VALUES (:name, :email, :phone, :address)
|
|
");
|
|
$stmt->execute([
|
|
':name' => $data['name'],
|
|
':email' => $data['email'],
|
|
':phone' => $data['phone'] ?? '',
|
|
':address' => $data['address'] ?? '',
|
|
]);
|
|
return (int)$db->lastInsertId();
|
|
}
|
|
|
|
public static function update(int $id, array $data): bool
|
|
{
|
|
$db = Database::getConnection();
|
|
$stmt = $db->prepare("
|
|
UPDATE customers
|
|
SET name = :name, email = :email, phone = :phone, address = :address
|
|
WHERE id = :id
|
|
");
|
|
return $stmt->execute([
|
|
':id' => $id,
|
|
':name' => $data['name'],
|
|
':email' => $data['email'],
|
|
':phone' => $data['phone'] ?? '',
|
|
':address' => $data['address'] ?? '',
|
|
]);
|
|
}
|
|
}
|