Skip to main content

Reservation Model

The Reservation model manages room reservation records (expedientes) in the system. It handles the creation, retrieval, and status management of reservations, as well as the association with materials and time slots.

Constructor

public function __construct()
Initializes the Reservation model and establishes a PDO database connection.

Methods

create()

Creates a new reservation record.
public function create(
    int $userId,
    int $roomId,
    string $eventName,
    ?string $notes = null
): int
userId
int
required
ID of the user creating the reservation
roomId
int
required
ID of the room being reserved
eventName
string
required
Name of the event for this reservation
notes
string
default:"null"
Optional notes or additional information about the reservation
Returns: int - The ID of the newly created reservation SQL Query:
INSERT INTO reservations (user_id, room_id, event_name, notes)
VALUES (:user_id, :room_id, :event_name, :notes)
Example:
$reservation = new Reservation();
$reservationId = $reservation->create(
    userId: 1,
    roomId: 3,
    eventName: 'Conferencia de Prensa',
    notes: 'Se requiere proyector y micrófonos'
);

findById()

Retrieves a single reservation by its ID with joined user and room information.
public function findById(int $id): ?array
id
int
required
The reservation ID to retrieve
Returns: ?array - Associative array with reservation data or null if not found
id
int
Reservation ID
user_id
int
ID of the user who created the reservation
room_id
int
ID of the reserved room
event_name
string
Name of the event
status
string
Reservation status: ‘pendiente’, ‘aprobado’, or ‘rechazado’
notes
string
Additional notes about the reservation
created_at
timestamp
Timestamp when the reservation was created
username
string
Username of the person who created the reservation
room_name
string
Name of the reserved room
SQL Query:
SELECT 
    r.id, r.user_id, r.room_id, r.event_name,
    r.status, r.notes, r.created_at,
    u.username, rm.name AS room_name
FROM reservations r
JOIN users u ON u.id = r.user_id
JOIN rooms rm ON rm.id = r.room_id
WHERE r.id = :id
LIMIT 1
Example:
$reservation = new Reservation();
$data = $reservation->findById(15);

if ($data) {
    echo "Event: " . $data['event_name'];
    echo "Room: " . $data['room_name'];
    echo "Status: " . $data['status'];
}

getAll()

Retrieves all reservations with basic information. Primarily used by the Comunicación y Difusión department.
public function getAll(): array
Returns: array - Array of associative arrays containing reservation data
id
int
Reservation ID
event_name
string
Name of the event
status
string
Current reservation status
created_at
timestamp
Creation timestamp
room_name
string
Name of the reserved room
username
string
Username of the requester
SQL Query:
SELECT 
    r.id, r.event_name, r.status, r.created_at,
    rm.name AS room_name, u.username
FROM reservations r
JOIN rooms rm ON rm.id = r.room_id
JOIN users u ON u.id = r.user_id
ORDER BY r.created_at DESC
Example:
$reservation = new Reservation();
$allReservations = $reservation->getAll();

foreach ($allReservations as $res) {
    echo "{$res['event_name']} - {$res['status']}\n";
}

updateStatus()

Updates the status of a reservation. Used by administrators to approve or reject reservations.
public function updateStatus(int $reservationId, string $status): bool
reservationId
int
required
The ID of the reservation to update
status
string
required
The new status. Must be one of: ‘pendiente’, ‘aprobado’, ‘rechazado’
Returns: bool - true if the update was successful Throws: InvalidArgumentException - If an invalid status is provided
The method validates that the status is one of the three allowed values before executing the update.
SQL Query:
UPDATE reservations
SET status = :status
WHERE id = :id
Example:
$reservation = new Reservation();

try {
    $reservation->updateStatus(15, 'aprobado');
    echo "Reservation approved successfully";
} catch (InvalidArgumentException $e) {
    echo "Invalid status: " . $e->getMessage();
}

attachMaterials()

Assigns materials to a reservation. Replaces any previously assigned materials.
public function attachMaterials(int $reservationId, array $materialIds): void
reservationId
int
required
The ID of the reservation
materialIds
array
required
Array of material IDs to assign to the reservation
Returns: void
This method:
  1. Deletes all existing material assignments for the reservation
  2. Normalizes the input array (removes duplicates, filters invalid IDs)
  3. Inserts new material assignments
SQL Queries:
-- Step 1: Delete existing assignments
DELETE FROM reservation_materials WHERE reservation_id = :id

-- Step 2: Insert new assignments
INSERT INTO reservation_materials (reservation_id, material_id)
VALUES (:reservation_id, :material_id)
Example:
$reservation = new Reservation();
$reservation->attachMaterials(15, [1, 3, 5, 7]);
// Assigns materials with IDs 1, 3, 5, and 7 to reservation 15

getMaterials()

Retrieves all materials assigned to a specific reservation.
public function getMaterials(int $reservationId): array
reservationId
int
required
The ID of the reservation
Returns: array - Array of associative arrays containing material data
id
int
Material ID
name
string
Material name
SQL Query:
SELECT m.id, m.name
FROM reservation_materials rm
JOIN materials m ON m.id = rm.material_id
WHERE rm.reservation_id = :reservation_id
ORDER BY m.name
Example:
$reservation = new Reservation();
$materials = $reservation->getMaterials(15);

foreach ($materials as $material) {
    echo "- {$material['name']}\n";
}

getByStatus()

Retrieves reservations filtered by their status.
public function getByStatus(string $status): array
status
string
required
The status to filter by: ‘pendiente’, ‘aprobado’, or ‘rechazado’
Returns: array - Array of reservations with the specified status, or empty array if invalid status
id
int
Reservation ID
event_name
string
Name of the event
status
string
Reservation status
created_at
timestamp
Creation timestamp
room_name
string
Name of the reserved room
username
string
Username of the requester
SQL Query:
SELECT 
    r.id, r.event_name, r.status, r.created_at,
    rm.name AS room_name, u.username
FROM reservations r
JOIN rooms rm ON rm.id = r.room_id
JOIN users u ON u.id = r.user_id
WHERE r.status = :status
ORDER BY r.created_at DESC
Example:
$reservation = new Reservation();
$pending = $reservation->getByStatus('pendiente');

echo "Pending reservations: " . count($pending);

getByUser()

Retrieves all reservations created by a specific user.
public function getByUser(int $userId): array
userId
int
required
The ID of the user
Returns: array - Array of reservations created by the user
id
int
Reservation ID
event_name
string
Name of the event
status
string
Current reservation status
created_at
timestamp
Creation timestamp
room_name
string
Name of the reserved room
SQL Query:
SELECT r.id, r.event_name, r.status, r.created_at,
       rm.name AS room_name
FROM reservations r
JOIN rooms rm ON rm.id = r.room_id
WHERE r.user_id = :user_id
ORDER BY r.created_at DESC
Example:
$reservation = new Reservation();
$userReservations = $reservation->getByUser(5);

foreach ($userReservations as $res) {
    echo "{$res['event_name']} ({$res['status']})\n";
}

getSlots()

Retrieves all time slots associated with a reservation.
public function getSlots(int $reservationId): array
reservationId
int
required
The ID of the reservation
Returns: array - Array of time slot data ordered by date and start time
date
string
Date of the time slot (YYYY-MM-DD)
start_time
string
Start time of the slot (HH:MM:SS)
end_time
string
End time of the slot (HH:MM:SS)
SQL Query:
SELECT date, start_time, end_time
FROM reservation_slots
WHERE reservation_id = :id
ORDER BY date, start_time
Example:
$reservation = new Reservation();
$slots = $reservation->getSlots(15);

foreach ($slots as $slot) {
    echo "{$slot['date']}: {$slot['start_time']} - {$slot['end_time']}\n";
}

Database Schema

The Reservation model interacts with the following database tables:
  • reservations - Main reservation records
  • reservation_slots - Time slots for each reservation
  • reservation_materials - Junction table linking reservations to materials
  • users - User information
  • rooms - Room information
  • materials - Material catalog

Status Values

Reservations can have one of three statuses:
  • pendiente - Pending approval
  • aprobado - Approved by Comunicación y Difusión
  • rechazado - Rejected

Build docs developers (and LLMs) love