Neuron-PHP

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

Methods

__construct()

Constructor

public __construct(SettingManager $settings) : mixed
Parameters
$settings : SettingManager

Settings manager with database configuration

Tags
throws
Exception

if database configuration is missing or adapter is unsupported

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
int

delete()

Delete a page

public delete(int $id) : bool
Parameters
$id : int

Page ID

Return values
bool

True if deleted, false otherwise

getByAuthor()

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>

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:

  1. Setting timestamps if not already set
  2. Saving the entity
  3. 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
template
throws
RepositoryException

If entity cannot be saved or found after save

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
template
throws
RepositoryException

If entity cannot be found after save

Return values
T

The refreshed entity from database


        
On this page

Search results