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; } }