Neuron-PHP

DatabaseTagRepository
in package
implements ITagRepository uses ManagesTimestamps

Database-backed tag repository using ORM.

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

Table of Contents

Interfaces

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

__sleep()

Handle serialization for PHPUnit process isolation

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

allWithPostCount()

Get tags with post count

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

Array of ['tag' => Tag, 'post_count' => int]

delete()

Delete a tag

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

findByName()

Find tag by name

public findByName(string $name) : Tag|null
Parameters
$name : string
Return values
Tag|null

findBySlug()

Find tag by slug

public findBySlug(string $slug) : Tag|null
Parameters
$slug : string
Return values
Tag|null

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