\Piko\DbRecord
DbRecord represents a database table’s row and implements the Active Record pattern.
Constants summary
Name | Description |
---|---|
public TYPE_BOOL |
|
public TYPE_INT |
|
public TYPE_STRING |
Properties summary
Name | Description |
---|---|
protected $data |
|
protected $db |
The database instance. |
protected $primaryKey |
The name of the primary key. Default to 'id'. |
protected $schema |
A name-value pair that describes the structure of … |
protected $tableName |
The name of the table. |
Inherited Properties
Name | Description |
---|---|
protected $errors |
Errors hash container |
protected $eventDispatcher |
|
protected $listenerProvider |
Methods summary
Name | Description |
---|---|
public __construct |
Constructor |
public __get |
Magick method to access rows's data as class attri… |
public __isset |
Magick method to check if attribute is defined in … |
public __set |
Magick method to set row's data as class attribute… |
public __unset |
Magick method to unset attribute in row's data. |
public delete |
Delete this record. |
public load |
Load row data. |
public quoteIdentifier |
Quote table or column name |
public save |
Save this record into the table. |
protected afterDelete |
Method called after a delete action. |
protected afterSave |
Method called after a save action. |
protected beforeDelete |
Method called before a delete action. |
protected beforeSave |
Method called before a save action. |
protected checkColumn |
Check if column name is defined in the table schem… |
protected getAttributes |
Retrieve the attributes representing the record in… |
protected initializeSchema |
Initialize the schema for the database table. This… |
private getSchemaType |
Inherited Methods
Name | Description |
---|---|
public bind |
Bind the data to the model attribubes. |
public getErrors |
Return the errors hash container |
public isValid |
Check if the model is valid |
public on |
Registers an event listener. |
public toArray |
Get the model data as an associative array. |
public trigger |
Trigger an event that may be listen by event liste… |
protected getAttributes |
Get the public properties reprenting the data mode… |
protected setError |
Set an error that will be appended to the errors c… |
protected validate |
Validate this model (Should be extended). Inherite… |
public $TYPE_BOOL
public $TYPE_INT
public $TYPE_STRING
Properties
protected $data : array<string,string|int|bool>
protected $db : \PDO
The database instance.
protected $primaryKey : string
The name of the primary key. Default to ‘id’.
protected $schema : int[]
A name-value pair that describes the structure of the table.
eg.['id' => self::TYPE_INT, 'name' => 'id' => self::TYPE_STRING]
protected $tableName : string
The name of the table.
Methods
public __construct(): mixed
public __construct(\PDO $db): mixed
Constructor
Parameters
$db : A PDO instance
Return:
mixed
public __get(): mixed
public __get(string $attribute): mixed
Magick method to access rows’s data as class attribute.
Parameters
$attribute : The attribute’s name.
Return:
mixed The attribute’s value.
public __isset(): mixed
public __isset(string $attribute): mixed
Magick method to check if attribute is defined in row’s data.
Parameters
$attribute : The attribute’s name.
Return:
mixed
public __set(): void
public __set(string $attribute, string|int|bool $value): void
Magick method to set row’s data as class attribute.
Parameters
$attribute : The attribute’s name.
$value : The attribute’s value.
public __unset(): mixed
public __unset(string $attribute): mixed
Magick method to unset attribute in row’s data.
Parameters
$attribute : The attribute’s name.
Return:
mixed
public delete(): bool
public delete(): bool
Delete this record.
throws \RuntimeException
Return:
bool
public load(): static
public load(\Piko\number $id): static
Load row data.
Parameters
$id : The value of the row primary key.
throws \RuntimeException
Return:
static
public quoteIdentifier(): string
public quoteIdentifier(string $identifier): string
Quote table or column name
Parameters
$identifier :
Return:
string
public save(): bool
public save(): bool
Save this record into the table.
throws \RuntimeException
Return:
bool
protected afterDelete(): void
protected afterDelete(): void
Method called after a delete action.
protected afterSave(): void
protected afterSave(): void
Method called after a save action.
protected beforeDelete(): bool
protected beforeDelete(): bool
Method called before a delete action.
Return:
bool
protected beforeSave(): bool
protected beforeSave(bool $insert): bool
Method called before a save action.
Parameters
$insert : If the row is a new record, the value will be true, otherwise, false.
Return:
bool
protected checkColumn(): void
protected checkColumn(string $name): void
Check if column name is defined in the table schema.
Parameters
$name :
throws \RuntimeException
see \Piko\DbRecord::$schema
protected getAttributes(): array<string,mixed>
protected getAttributes(): array<string,mixed>
Retrieve the attributes representing the record in the database. This method returns an associative array where each key corresponds to a column name as defined in the schema, and each value is the respective column’s value from the current instance’s data. This can be particularly useful for debugging or when you need to serialize the record for storage or transmission.
Return:
array<string,mixed> An associative array where keys are column names and values are column values.
protected initializeSchema(): void
protected initializeSchema(): void
Initialize the schema for the database table.
This method uses reflection to inspect the current class for properties that have the FieldAttribute
attribute.
It then builds the schema array, which describes the structure of the table, using these properties.
Additionally, it sets the table name if a TableAttribute
is present on the class and identifies
the primary key based on field attributes.
private getSchemaType(): int
private getSchemaType(string $type): int
Parameters
$type :
Return:
int