Pickles Framework Documentation

Model
in package

AbstractYes

Table of Contents

Properties

$attributes  : array<string|int, mixed>
$fillable  : array<string|int, mixed>
$hidden  : array<string|int, mixed>
$insertTimestamps  : bool
$primaryKey  : string
$table  : string|null
$driver  : DatabaseDriver|null

Methods

__call()  : mixed
__construct()  : mixed
__get()  : mixed
__set()  : mixed
__sleep()  : array<string|int, mixed>
Prepares the object for serialization by removing hidden attributes and returning the list of object properties to serialize.
all()  : array<string|int, static>
Retrieve all records from the database table associated with the model.
create()  : static
Creates a new instance of the model and assigns the given attributes to it.
delete()  : static
Deletes the current record from the database based on its primary key.
find()  : static|null
Finds a record in the database by its primary key.
first()  : static|null
Retrieves the first record from the database model.
firstWhere()  : static|null
Retrieves the first record from the database where the specified column matches the given value.
mapModelsToObjects()  : array<string|int, mixed>
Maps an array of models to an array of objects.
save()  : static
Saves the current model instance to the database.
setDatabaseDriver()  : void
Sets the database driver instance to be used by the model.
update()  : static
Updates the current model instance in the database with the attributes set on the model.
where()  : array<string|int, static>
Filters records based on the specified column and value.
massAssign()  : static
Mass assigns the given attributes to the model if they are fillable.
setAttributes()  : static
Sets the attributes for the model.
toArray()  : array<string|int, mixed>
Converts the model's attributes to an array, excluding hidden attributes.
checkDriver()  : void
Checks if the database driver is set.
checkTimestamps()  : void
mapRowsToModels()  : array<string|int, static>
Maps an array of database rows to an array of model instances.

Properties

$attributes

protected array<string|int, mixed> $attributes = []

$fillable

protected array<string|int, mixed> $fillable = []

$hidden

protected array<string|int, mixed> $hidden = []

$insertTimestamps

protected bool $insertTimestamps = false

$primaryKey

protected string $primaryKey = 'id'

$table

protected string|null $table = null

Methods

__call()

public __call(mixed $method, mixed $args) : mixed
Parameters
$method : mixed
$args : mixed

__construct()

public __construct() : mixed

__get()

public __get(mixed $name) : mixed
Parameters
$name : mixed

__set()

public __set(mixed $name, mixed $value) : mixed
Parameters
$name : mixed
$value : mixed

__sleep()

Prepares the object for serialization by removing hidden attributes and returning the list of object properties to serialize.

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

This method is typically called when the object is being serialized (e.g., using serialize() function). It ensures that any attributes specified in the $hidden array are excluded from the serialized data.

Return values
array<string|int, mixed>

An array of property names to be serialized.

all()

Retrieve all records from the database table associated with the model.

public static all() : array<string|int, static>
Return values
array<string|int, static>

An array of all records as associative arrays or model instances.

create()

Creates a new instance of the model and assigns the given attributes to it.

public static create(array<string|int, mixed> $attributes) : static
Parameters
$attributes : array<string|int, mixed>

An associative array of attributes to assign to the model.

Return values
static

A new instance of the model with the provided attributes assigned.

delete()

Deletes the current record from the database based on its primary key.

public delete() : static
Tags
throws
PrimaryKeyNotSetException

If the primary key is not set or found in the attributes.

Return values
static

Returns the current instance of the model.

find()

Finds a record in the database by its primary key.

public static find(int|string $id) : static|null
Parameters
$id : int|string

The primary key of the record to find.

Return values
static|null

The found record as an instance of the calling class, or null if not found.

first()

Retrieves the first record from the database model.

public static first() : static|null
Return values
static|null

Returns the first record as an instance of the model, or null if no records are found.

firstWhere()

Retrieves the first record from the database where the specified column matches the given value.

public static firstWhere(string $column, mixed $value) : static|null
Parameters
$column : string

The name of the column to filter by.

$value : mixed

The value to match against the specified column.

Return values
static|null

An instance of the model with the matched record's attributes, or null if no match is found.

mapModelsToObjects()

Maps an array of models to an array of objects.

public static mapModelsToObjects(array<string|int, mixed> $models) : array<string|int, mixed>
Parameters
$models : array<string|int, mixed>

An array of models to be mapped.

Return values
array<string|int, mixed>

An array of objects mapped from the provided models.

save()

Saves the current model instance to the database.

public save() : static

This method constructs an SQL INSERT query using the model's attributes and executes it using the database driver. The attributes of the model are used as column names and their corresponding values are inserted into the database table.

Return values
static

Returns the current instance of the model.

setDatabaseDriver()

Sets the database driver instance to be used by the model.

public static setDatabaseDriver(DatabaseDriver $driver) : void
Parameters
$driver : DatabaseDriver

The database driver instance to set.

update()

Updates the current model instance in the database with the attributes set on the model.

public update() : static

If the insertTimestamps property is true, the updated_at timestamp is automatically set to the current date and time before updating the record.

Tags
throws
PrimaryKeyNotSetException

If the primary key is not set in the model's attributes.

Return values
static

Returns the current instance of the model after the update operation.

where()

Filters records based on the specified column and value.

public static where(string $column, mixed $value[, string $operator = "=" ]) : array<string|int, static>
Parameters
$column : string

The name of the column to filter by.

$value : mixed

The value to match against the specified column.

$operator : string = "="
Return values
array<string|int, static>

An array of records that match the specified condition.

massAssign()

Mass assigns the given attributes to the model if they are fillable.

protected massAssign(array<string|int, mixed> $attributes) : static
Parameters
$attributes : array<string|int, mixed>

An associative array of attributes to assign to the model.

Tags
throws
NoFillableAttributesDefinedException

If no fillable attributes are defined for the model.

throws
NoFillableAttributesProvidedException

If no attributes are provided for assignment.

Return values
static

Returns the current instance of the model after assigning the attributes.

setAttributes()

Sets the attributes for the model.

protected setAttributes(array<string|int, mixed> $attributes) : static
Parameters
$attributes : array<string|int, mixed>

An associative array of attributes to set.

Return values
static

Returns the current instance of the model with the attributes set.

toArray()

Converts the model's attributes to an array, excluding hidden attributes.

protected toArray() : array<string|int, mixed>

This method returns an associative array of the model's attributes, excluding any attributes specified in the $hidden property.

Return values
array<string|int, mixed>

The filtered array of attributes.

checkDriver()

Checks if the database driver is set.

private checkDriver() : void

This method verifies whether the static $driver property is initialized. If the driver is not set, it throws a NoDriverSetException.

Tags
throws
NoDriverSetException

If the database driver is not set.

checkTimestamps()

private checkTimestamps() : void

mapRowsToModels()

Maps an array of database rows to an array of model instances.

private mapRowsToModels(Model $model, array<string|int, mixed> $rows) : array<string|int, static>
Parameters
$model : Model

The model instance used as a template for mapping rows.

$rows : array<string|int, mixed>

An array of associative arrays representing database rows.

Return values
array<string|int, static>

An array of model instances populated with data from the rows.


        
On this page

Search results