DatabasePageRepository
in package
implements
IPageRepository
uses
ManagesTimestamps
Database-backed page repository using ORM.
Works with SQLite, MySQL, and PostgreSQL via the Neuron ORM.
Table of Contents
Interfaces
- IPageRepository
- Page repository interface.
Properties
- $_pdo : PDO
Methods
- __construct() : mixed
- Constructor
- all() : array<string|int, Page>
- Get all pages
- count() : int
- Count total pages
- create() : Page
- Create a new page
- delete() : bool
- Delete a page
- findById() : Page|null
- Find page by ID
- findBySlug() : Page|null
- Find page by slug
- getByAuthor() : array<string|int, Page>
- Get pages by author
- getDrafts() : array<string|int, Page>
- Get draft pages
- getPublished() : array<string|int, Page>
- Get published pages
- incrementViewCount() : bool
- Increment page view count
- update() : bool
- Update an existing page
- createEntity() : T
- Prepare entity for creation by setting timestamps, saving, and refreshing
- ensureTimestamps() : void
- Set created_at and updated_at timestamps if not already set
- saveAndRefresh() : T
- Save entity and return the refreshed version from database
Properties
$_pdo
private
PDO
$_pdo
Methods
__construct()
Constructor
public
__construct(SettingManager $settings) : mixed
Parameters
- $settings : SettingManager
-
Settings manager with database configuration
Tags
all()
Get all pages
public
all([string|null $status = null ][, int $limit = 0 ][, int $offset = 0 ]) : array<string|int, Page>
Parameters
- $status : string|null = null
-
Filter by status (null for all)
- $limit : int = 0
-
Maximum number of pages (0 for no limit)
- $offset : int = 0
-
Offset for pagination
Return values
array<string|int, Page>count()
Count total pages
public
count([string|null $status = null ]) : int
Parameters
- $status : string|null = null
-
Filter by status (null for all)
Return values
intcreate()
Create a new page
public
create(Page $page) : Page
Parameters
- $page : Page
-
Page to create
Return values
Page —Created page with ID
delete()
Delete a page
public
delete(int $id) : bool
Parameters
- $id : int
-
Page ID
Return values
bool —True if deleted, false otherwise
findById()
Find page by ID
public
findById(int $id) : Page|null
Parameters
- $id : int
-
Page ID
Return values
Page|nullfindBySlug()
Find page by slug
public
findBySlug(string $slug) : Page|null
Parameters
- $slug : string
-
Page slug
Return values
Page|nullgetByAuthor()
Get pages by author
public
getByAuthor(int $authorId[, string|null $status = null ]) : array<string|int, Page>
Parameters
- $authorId : int
-
Author user ID
- $status : string|null = null
-
Filter by status (null for all)
Return values
array<string|int, Page>getDrafts()
Get draft pages
public
getDrafts() : array<string|int, Page>
Return values
array<string|int, Page>getPublished()
Get published pages
public
getPublished([int $limit = 0 ][, int $offset = 0 ]) : array<string|int, Page>
Parameters
- $limit : int = 0
-
Maximum number of pages (0 for no limit)
- $offset : int = 0
-
Offset for pagination
Return values
array<string|int, Page>incrementViewCount()
Increment page view count
public
incrementViewCount(int $id) : bool
Uses atomic UPDATE to avoid race condition under concurrent requests.
Parameters
- $id : int
-
Page ID
Return values
bool —True if updated, false otherwise
update()
Update an existing page
public
update(Page $page) : bool
Parameters
- $page : Page
-
Page to update
Return values
bool —True if updated, false otherwise
createEntity()
Prepare entity for creation by setting timestamps, saving, and refreshing
protected
createEntity(T $entity, callable $finder, string $entityType) : T
This combines the common pattern of:
- Setting timestamps if not already set
- Saving the entity
- Re-fetching from database to get all DB-generated values
Use this in create() methods after performing duplicate checks.
Parameters
- $entity : T
-
Entity to create
- $finder : callable
-
Callback to find entity by ID: function(int $id): ?T
- $entityType : string
-
Human-readable entity type for error messages
Tags
Return values
T —The refreshed entity from database
ensureTimestamps()
Set created_at and updated_at timestamps if not already set
protected
ensureTimestamps(object $entity) : void
Parameters
- $entity : object
-
Entity with getCreatedAt/setCreatedAt and getUpdatedAt/setUpdatedAt methods
saveAndRefresh()
Save entity and return the refreshed version from database
protected
saveAndRefresh(T $entity, callable $finder, string $entityType) : T
Ensures the returned entity has all database-generated values. Throws an exception if the entity cannot be found after save.
Parameters
- $entity : T
-
Entity to save
- $finder : callable
-
Callback to find entity by ID: function(int $id): ?T
- $entityType : string
-
Human-readable entity type for error messages
Tags
Return values
T —The refreshed entity from database