EventRepository
in package
EventRepository - Data Mapper for Event entities
This repository implements the Repository Pattern and Data Mapper Pattern, providing a clean separation between the domain layer (Event entities) and the database layer (PDO).
Uses EventFactory to transform PDO arrays into Event entities.
Tags
Table of Contents
Properties
- $pdo : PDO
- PDO database connection instance
Methods
- __construct() : mixed
- Constructor - Dependency Injection of PDO connection
- count() : int
- Count total number of events
- delete() : bool
- Delete an event by its ID
- findAll() : array<int, Event>
- Retrieve all events
- findById() : Event|null
- Find an event by its unique identifier
- findBySlug() : Event|null
- Find an event by its SEO-friendly slug
- findEventsForHomepage() : array<int, Event>
- Find events for homepage carousel: upcoming first, then recent past as fallback
- findLatestEvents() : array<int, Event>
- Retrieve upcoming events (future events only)
- findPaginated() : array<int, Event>
- Retrieve paginated events
- save() : bool
- Save an event (insert or update)
- updateEventImages() : bool
- Update only the images associated with an event
- insert() : bool
- Insert a new event into the database
- slugExists() : bool
- Check if a slug already exists in the database
- slugExistsExcludingId() : bool
- Check if a slug exists excluding a specific event ID
- update() : bool
- Update an existing event in the database
Properties
$pdo
PDO database connection instance
private
PDO
$pdo
Methods
__construct()
Constructor - Dependency Injection of PDO connection
public
__construct(PDO $pdo) : mixed
Following best practices (CM4 Slide 22), the PDO connection is injected via constructor to facilitate testing and respect the Dependency Inversion Principle.
Parameters
- $pdo : PDO
-
Database connection instance
count()
Count total number of events
public
count() : int
Return values
int —Total event count
delete()
Delete an event by its ID
public
delete(int $id) : bool
Parameters
- $id : int
-
Event ID to delete
Return values
bool —True if deletion succeeded, false otherwise
findAll()
Retrieve all events
public
findAll() : array<int, Event>
Return values
array<int, Event> —Array of Event entities
findById()
Find an event by its unique identifier
public
findById(int $id) : Event|null
Parameters
- $id : int
-
Event ID
Return values
Event|null —Event entity or null if not found
findBySlug()
Find an event by its SEO-friendly slug
public
findBySlug(string $slug) : Event|null
Parameters
- $slug : string
-
URL-friendly slug
Return values
Event|null —Event entity or null if not found
findEventsForHomepage()
Find events for homepage carousel: upcoming first, then recent past as fallback
public
findEventsForHomepage([int $limit = 5 ]) : array<int, Event>
Ensures the carousel is never empty when there are no future events:
- Prefer events with event_date >= today (ordered by date ascending).
- If fewer than limit, complete with most recent past events (ordered by date descending).
Parameters
- $limit : int = 5
-
Maximum number of events to return (default: 5)
Return values
array<int, Event> —Array of Event entities
findLatestEvents()
Retrieve upcoming events (future events only)
public
findLatestEvents(int $limit) : array<int, Event>
Parameters
- $limit : int
-
Maximum number of events to retrieve
Return values
array<int, Event> —Array of upcoming Event entities
findPaginated()
Retrieve paginated events
public
findPaginated(int $offset, int $limit) : array<int, Event>
Parameters
- $offset : int
-
Starting offset
- $limit : int
-
Number of events to retrieve
Return values
array<int, Event> —Array of Event entities
save()
Save an event (insert or update)
public
save(Event $event) : bool
If the event has no ID (null), it will be inserted. If the event has an ID, it will be updated.
This method implements the "smart save" pattern recommended for clean controller code.
Parameters
- $event : Event
-
Event entity to save
Return values
bool —True if save succeeded, false otherwise
updateEventImages()
Update only the images associated with an event
public
updateEventImages(int $eventId, string $imageJson) : bool
Useful for image upload operations without modifying other data.
Parameters
- $eventId : int
-
Event unique identifier
- $imageJson : string
-
JSON string of image URLs
Return values
bool —True if update succeeded, false otherwise
insert()
Insert a new event into the database
private
insert(Event $event) : bool
Parameters
- $event : Event
-
Event entity to insert
Return values
bool —True if insertion succeeded, false otherwise
slugExists()
Check if a slug already exists in the database
private
slugExists(string $slug) : bool
Parameters
- $slug : string
-
The slug to check
Return values
bool —True if exists, false otherwise
slugExistsExcludingId()
Check if a slug exists excluding a specific event ID
private
slugExistsExcludingId(string $slug, int $excludeId) : bool
Parameters
- $slug : string
-
The slug to check
- $excludeId : int
-
Event ID to exclude from check
Return values
bool —True if exists, false otherwise
update()
Update an existing event in the database
private
update(Event $event) : bool
Parameters
- $event : Event
-
Event entity to update
Return values
bool —True if update succeeded, false otherwise