API Reference

Model (Abstract)

Namespace: WPZylos\Framework\Model

The base class for all models. Extends this to create your own model classes.

Static Properties

PropertyTypeDefaultDescription
$tablestring''Table name without WP prefix
$primaryKeystring'id'Primary key column name
$app?ApplicationInterfacenullApplication instance

Instance Properties

PropertyTypeDefaultDescription
$fillablearray[]Mass-assignable attributes
$guardedarray['id']Non-mass-assignable attributes
$castsarray[]Attribute type casts
$hiddenarray[]Hidden from serialization
$timestampsbooltrueAuto-manage timestamps
$createdAtColumn?string'created_at'Created timestamp column
$updatedAtColumn?string'updated_at'Updated timestamp column
$existsboolfalseWhether record exists in DB

Static Methods

setApplication(ApplicationInterface $app): void

Set the application instance for all models.

getApplication(): ApplicationInterface

Get the application instance. Throws RuntimeException if not set.

resolveTableName(): string

Get the full table name with WordPress prefix.

getPrimaryKey(): string

Get the primary key column name.

query(): QueryBuilder

Create a new QueryBuilder for this model's table with global scopes applied.

newQueryWithoutScopes(): QueryBuilder

Create a raw QueryBuilder without any global scopes.

where(string $column, mixed $operator, mixed $value = null): QueryBuilder

Proxy static where() to a new query builder.

all(): ModelCollection

Get all records as a ModelCollection.

find(int|string $id): ?static

Find a model by its primary key. Returns null if not found.

findOrFail(int|string $id): static

Find a model by primary key or throw RuntimeException.

create(array $attributes): static

Create a new model instance and persist it.

newFromRow(object $row): static

Create a model instance from a database row (stdClass).

Instance Methods

__construct(array $attributes = [])

Create a new model instance with optional initial attributes.

fill(array $attributes): static

Fill the model with attributes, respecting $fillable / $guarded.

forceFill(array $attributes): static

Fill the model ignoring guarded attributes.

getKey(): mixed

Get the primary key value.

save(): bool

Persist the model (insert or update).

update(array $attributes): bool

Update the model with the given attributes and save.

delete(): bool

Delete the model from the database.

toArray(): array

Convert the model to an array, excluding $hidden attributes.

toJson(int $options = 0): string

Convert the model to a JSON string.

jsonSerialize(): array

Implements JsonSerializable.


HasAttributes (Trait)

Namespace: WPZylos\Framework\Model\Concerns

Methods

getAttribute(string $key): mixed

Get an attribute value with accessor and casting support.

setAttribute(string $key, mixed $value): static

Set an attribute value with mutator support.

getAttributes(): array

Get all raw attributes.

getOriginal(): array

Get the original attributes snapshot.

syncOriginal(): static

Sync original attributes to current state.

getDirty(): array

Get attributes that have changed since last sync.

isDirty(?string $key = null): bool

Check if the model or a specific attribute is dirty.


HasTimestamps (Trait)

Namespace: WPZylos\Framework\Model\Concerns

Methods

touch(): bool

Touch the updated_at timestamp and persist.

freshTimestamp(): string (protected)

Get the current MySQL-formatted timestamp.

touchCreatedAt(): void (protected)

Set the created_at timestamp.

touchUpdatedAt(): void (protected)

Set the updated_at timestamp.

touchTimestamps(): void (protected)

Set both created_at and updated_at.


HasEvents (Trait)

Namespace: WPZylos\Framework\Model\Concerns

Methods

registerModelEvent(string $event, callable $callback): void

Register an event listener for this model class.

creating(callable $callback): void

Register a "creating" event listener.

created(callable $callback): void

Register a "created" event listener.

updating(callable $callback): void

Register an "updating" event listener.

updated(callable $callback): void

Register an "updated" event listener.

deleting(callable $callback): void

Register a "deleting" event listener.

deleted(callable $callback): void

Register a "deleted" event listener.

saving(callable $callback): void

Register a "saving" event listener (fires on create and update).

saved(callable $callback): void

Register a "saved" event listener (fires on create and update).

flushEventListeners(): void

Clear all registered events for this model class.


HasRelationships (Trait)

Namespace: WPZylos\Framework\Model\Concerns

Methods

Define a has-one relationship.

  • $related — Related model class name
  • $foreignKey — Foreign key on related table (default: {thismodel}_id)
  • $localKey — Local key (default: primary key)

Define a has-many relationship.

Define a belongs-to (inverse) relationship.

  • $foreignKey — Foreign key on this table (default: {related}_id)
  • $ownerKey — Owner key on related table (default: related primary key)

setRelation(string $name, mixed $value): static

Set a relation value on the model.


SoftDeletes (Trait)

Namespace: WPZylos\Framework\Model\Concerns

Properties

PropertyTypeDefaultDescription
$deletedAtColumnstring'deleted_at'Soft delete column

Methods

trashed(): bool

Check if the model is soft-deleted.

delete(): bool

Soft-delete by setting deleted_at timestamp.

restore(): bool

Restore a soft-deleted model.

forceDelete(): bool

Permanently delete the model from the database.

withTrashed(): QueryBuilder (static)

Get a query that includes trashed records.

onlyTrashed(): QueryBuilder (static)

Get a query that returns only trashed records.

Lifecycle Events

SoftDeletes fires additional model events that can be registered via registerModelEvent():

  • restoring — Before a soft-deleted model is restored (can cancel by returning false)
  • restored — After a soft-deleted model is restored
  • forceDeleting — Before a model is permanently deleted (can cancel by returning false)
  • forceDeleted — After a model is permanently deleted

ModelCollection

Namespace: WPZylos\Framework\Model

Implements: ArrayAccess, Countable, IteratorAggregate, JsonSerializable

Methods

__construct(array $items = [])

Create a new collection.

all(): array

Get all items as a plain array.

first(): mixed

Get the first item, or null if empty.

last(): mixed

Get the last item, or null if empty.

count(): int

Get the number of items.

isEmpty(): bool

Check if the collection is empty.

isNotEmpty(): bool

Check if the collection is not empty.

pluck(string $key): array

Extract a single attribute from each item.

map(callable $callback): static

Apply a callback to each item. Returns a new collection.

filter(?callable $callback = null): static

Filter items. Returns a new collection.

each(callable $callback): static

Run a callback on each item. Return false to stop.

contains(callable $callback): bool

Check if any item matches the callback.

toArray(): array

Convert all items to arrays.

toJson(int $options = 0): string

Convert to JSON string.


ModelServiceProvider

Namespace: WPZylos\Framework\Model

Extends: WPZylos\Framework\Core\ServiceProvider

Methods

register(ApplicationInterface $app): void

Registers ModelResolver as a singleton (model.resolver).

boot(ApplicationInterface $app): void

Injects the application instance into the base Model class.


ModelResolver

Namespace: WPZylos\Framework\Model

Methods

__construct(ApplicationInterface $app)

Create the resolver with an application instance.

resolve(string $modelClass, array $attributes = []): Model

Create a new model instance by class name.

find(string $modelClass, int|string $id): ?Model

Find a model by class and primary key.

getApplication(): ApplicationInterface

Get the application instance.