Neuron-PHP

DatabasePostRepository
in package
implements IPostRepository uses ManagesTimestamps

Database-backed post repository using ORM.

Works with SQLite, MySQL, and PostgreSQL via the Neuron ORM.

Table of Contents

Interfaces

IPostRepository
Post repository interface.

Properties

$_pdo  : PDO

Methods

__construct()  : mixed
Constructor
__sleep()  : array<string|int, mixed>
Handle serialization for PHPUnit process isolation
__wakeup()  : void
Handle unserialization for PHPUnit process isolation
all()  : array<string|int, Post>
Get all posts
attachCategories()  : bool
Attach categories to post
attachTags()  : bool
Attach tags to post
count()  : int
Count total posts
create()  : Post
Create a new post
delete()  : bool
Delete a post
detachCategories()  : bool
Detach all categories from post
detachTags()  : bool
Detach all tags from post
findById()  : Post|null
Find post by ID
findBySlug()  : Post|null
Find post by slug
getByAuthor()  : array<string|int, Post>
Get posts by author
getByCategory()  : array<string|int, Post>
Get posts by category
getByTag()  : array<string|int, Post>
Get posts by tag
getDrafts()  : array<string|int, Post>
Get draft posts
getPublished()  : array<string|int, Post>
Get published posts
getScheduled()  : array<string|int, Post>
Get scheduled posts
incrementViewCount()  : bool
Increment post view count atomically
update()  : bool
Update an existing post
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
getOrmPdo()  : PDO
Get the PDO connection from the ORM
loadRelationsWithPdo()  : void
Load relations using a specific PDO connection
syncCategoriesWithPdo()  : void
Sync categories using a specific PDO connection
syncTagsWithPdo()  : void
Sync tags using a specific PDO connection

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

__sleep()

Handle serialization for PHPUnit process isolation

public __sleep() : array<string|int, mixed>
Return values
array<string|int, mixed>

all()

Get all posts

public all([string|null $status = null ][, int $limit = 0 ][, int $offset = 0 ]) : array<string|int, Post>
Parameters
$status : string|null = null

Filter by status (published, draft, scheduled)

$limit : int = 0

Limit number of results (0 = no limit)

$offset : int = 0

Offset for pagination

Return values
array<string|int, Post>

attachCategories()

Attach categories to post

public attachCategories(int $postId, array<string|int, mixed> $categoryIds) : bool
Parameters
$postId : int

Post ID

$categoryIds : array<string|int, mixed>

Array of category IDs

Return values
bool

attachTags()

Attach tags to post

public attachTags(int $postId, array<string|int, mixed> $tagIds) : bool
Parameters
$postId : int

Post ID

$tagIds : array<string|int, mixed>

Array of tag IDs

Return values
bool

count()

Count total posts

public count([string|null $status = null ]) : int
Parameters
$status : string|null = null

Filter by status

Return values
int

detachCategories()

Detach all categories from post

public detachCategories(int $postId) : bool
Parameters
$postId : int
Return values
bool

detachTags()

Detach all tags from post

public detachTags(int $postId) : bool
Parameters
$postId : int
Return values
bool

getByAuthor()

Get posts by author

public getByAuthor(int $authorId[, string|null $status = null ]) : array<string|int, Post>
Parameters
$authorId : int

Author user ID

$status : string|null = null

Filter by status

Return values
array<string|int, Post>

getByCategory()

Get posts by category

public getByCategory(int $categoryId[, string|null $status = null ]) : array<string|int, Post>
Parameters
$categoryId : int

Category ID

$status : string|null = null

Filter by status

Return values
array<string|int, Post>

getByTag()

Get posts by tag

public getByTag(int $tagId[, string|null $status = null ]) : array<string|int, Post>
Parameters
$tagId : int

Tag ID

$status : string|null = null

Filter by status

Return values
array<string|int, Post>

getPublished()

Get published posts

public getPublished([int $limit = 0 ][, int $offset = 0 ]) : array<string|int, Post>
Parameters
$limit : int = 0

Limit number of results (0 = no limit)

$offset : int = 0

Offset for pagination

Return values
array<string|int, Post>

incrementViewCount()

Increment post view count atomically

public incrementViewCount(int $id) : bool

Uses atomic SQL UPDATE via ORM to avoid race conditions under concurrent requests. The fetch-increment-save pattern would lose increments under high concurrency.

Parameters
$id : int
Return values
bool

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

getOrmPdo()

Get the PDO connection from the ORM

private getOrmPdo() : PDO

This method uses reflection to access the static PDO connection stored in the Model base class. The PDO connection is needed for transaction-scoped operations within Model::transaction().

Tags
throws
RuntimeException

If the PDO connection cannot be retrieved or is not available

Return values
PDO

The PDO connection used by the ORM

loadRelationsWithPdo()

Load relations using a specific PDO connection

private loadRelationsWithPdo(PDO $pdo, Post $post) : void
Parameters
$pdo : PDO
$post : Post

syncCategoriesWithPdo()

Sync categories using a specific PDO connection

private syncCategoriesWithPdo(PDO $pdo, int $postId, array<string|int, mixed> $categoryIds) : void
Parameters
$pdo : PDO
$postId : int
$categoryIds : array<string|int, mixed>

syncTagsWithPdo()

Sync tags using a specific PDO connection

private syncTagsWithPdo(PDO $pdo, int $postId, array<string|int, mixed> $tagIds) : void
Parameters
$pdo : PDO
$postId : int
$tagIds : array<string|int, mixed>

        
On this page

Search results