Neuron-PHP

DatabaseCategoryRepository
in package
implements ICategoryRepository uses ManagesTimestamps

Database-backed category repository using ORM.

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

Table of Contents

Interfaces

ICategoryRepository
Category 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, Category>
Get all categories
allWithPostCount()  : array<string|int, mixed>
Get categories with post count
count()  : int
Count total categories
create()  : Category
Create a new category
delete()  : bool
Delete a category
findById()  : Category|null
Find category by ID
findByIds()  : array<string|int, Category>
Find multiple categories by IDs
findByName()  : Category|null
Find category by name
findBySlug()  : Category|null
Find category by slug
update()  : bool
Update an existing category
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

__sleep()

Handle serialization for PHPUnit process isolation

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

allWithPostCount()

Get categories with post count

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

Array of ['category' => Category, 'post_count' => int]

findByIds()

Find multiple categories by IDs

public findByIds(array<string|int, mixed> $ids) : array<string|int, Category>
Parameters
$ids : array<string|int, mixed>

Array of category IDs

Return values
array<string|int, Category>

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