Repository navigation
Internals Model Model
Framework Internals · Subsystem overview · Developer guide
Source: awt_src/classes/model/Model.php · Snapshot 8411f4287706.
Models add hydration, persistence, relations, casts and attribute controls to DatabaseManager. Collections hydrate already fetched rows and provide iteration/counting.
use database\DatabaseManager;
use model\exceptions\ModelCreationException;
use model\exceptions\ModelCRUDException;
use model\interfaces\IRelationBelongs;
use model\interfaces\IRelationHasMany;
use model\interfaces\IRelationWith;
use ReflectionClass;
use ReflectionProperty;
use Throwable;abstract class Model extends DatabaseManager implements \JsonSerializableSource description (behavioral corrections are recorded in Known discrepancies):
Model Class
This abstract class extends the DatabaseManager to provide common functionality for all models, including methods for retrieving records from the database by ID or retrieving all records from a table. It serves as a base class for specific models that represent database entities.
Declarations below preserve visibility, type, default and modifiers exactly. A typed property without an initializer remains uninitialized until its constructor or another method assigns it. Mutable fields belong to this instance unless marked static; readonly fields can be initialized once. Constructor-promoted properties are also listed below.
| Member | Declaration | Purpose or source contract |
|---|---|---|
| $model_source | public ?string $model_source = null; |
Used by selectByID(), loadHasMany(), selectAll(), save(), saveModel(), deleteModel(), find(), hydrateRow(), newQuery(), getModels(). |
| $model_id | protected ?int $model_id = null; |
Used by loadHasMany(), setModelId(), save(), saveModel(), deleteModel(), hydrateRow(), clearLoadedAttributes(), existsInDatabase(), getKey(). |
| $id_column | public ?string $id_column = null; |
Used by selectByID(), loadHasMany(), save(), saveModel(), deleteModel(), hydrateRow(), getModels(). |
| $dynamicData | public array $dynamicData = []; |
Used by getParam(), assignAttribute(), clearLoadedAttributes(), __toArray(), __isset(), __get(). |
| $paramBlackList | protected array $paramBlackList = ["tables", "model_source", "id_column"]; |
Used by paramBlackList(), persistenceAttributes(). |
| $ormOriginal | private array $ormOriginal = []; |
Initializes the Model by calling the parent constructor of DatabaseManager. |
| $ormMissingAttributes | private array $ormMissingAttributes = []; |
Used by assignAttribute(), clearLoadedAttributes(), __toArray(), __get(). |
| $ormLoadedKeys | private array $ormLoadedKeys = []; |
Used by hydrateRow(), clearLoadedAttributes(). |
| $ormLoading | private static array $ormLoading = []; |
Used by hydrateRow(). |
| $ormRelationKeys | private array $ormRelationKeys = []; |
Used by loadHasMany(), loadRelationObject(), clearLoadedAttributes(), persistenceAttributes(), __get(). |
| $ormLastChanges | private array $ormLastChanges = []; |
Used by save(), saveModel(), clearLoadedAttributes(), getChanges(). |
| Method | Signature |
|---|---|
| __construct | public function __construct( ?\database\interface\IProvider $provider = null, ?\database\query\QueryBuilder $builder = null, ?\database\interface\ICache $cache = null, ) |
| selectByID | final public function selectByID(?int $id, string $table = '', string $column = ''): void |
| inferTableName | public function inferTableName(): string |
| loadWith | public function loadWith(array $row): void |
| loadBelongsTo | public function loadBelongsTo(array $row): void |
| loadHasMany | public function loadHasMany(array $row): void |
| loadRelationObject | protected function loadRelationObject(array $relation, array $row): void |
| createRelationObject | protected function createRelationObject(array $relation, array $row = []): ?object |
| selectAll | final protected function selectAll(string $table = ''): array |
| getParam | final public function getParam(string $key): mixed |
| setModelId | public function setModelId(int $id): void |
| save | public function save(): bool |
| saveModel | public function saveModel(): int|null |
| deleteModel | public function deleteModel(): bool |
| find | public function find(string $column, mixed $value, ?string $source = null): array |
| paramBlackList | public function paramBlackList(string $key): void |
| camelToSnake | protected function camelToSnake(string $input): string |
| isSnakeCase | private function isSnakeCase(string $input): bool |
| fromArray | public function fromArray(array $data): void |
| fill | public function fill(array $data): static |
| fillableAttributes | protected function fillableAttributes(): array |
| hiddenAttributes | protected function hiddenAttributes(): array |
| attributeCasts | protected function attributeCasts(): array |
| writableAttributes | protected function writableAttributes(): ?array |
| assignAttribute | private function assignAttribute(string $key, mixed $value): void |
| hydrateRow | public function hydrateRow(array $row, ?string $table = null, ?string $key = null): static |
| clearLoadedAttributes | private function clearLoadedAttributes(): void |
| persistenceAttributes | private function persistenceAttributes(): array |
| newQuery | public function newQuery(): static |
| getModels | public function getModels(?int $limit = null, ?int $offset = null): array |
| firstModel | public function firstModel(): ?static |
| firstModelOrFail | public function firstModelOrFail(): static |
| syncOriginal | public function syncOriginal(): static |
| getDirty | public function getDirty(): array |
| isDirty | public function isDirty(?string $attribute = null): bool |
| getChanges | public function getChanges(): array |
| existsInDatabase | public function existsInDatabase(): bool |
| getKey | public function getKey(): ?int |
| toArray | public function toArray(): array |
| jsonSerialize | public function jsonSerialize(): array |
| __toString | public function __toString(): string |
| __toArray | public function __toArray(): array |
| __isset | public function __isset(string $name): bool |
| __get | public function &__get(string $name): mixed |
| __set | public function __set(string $name, mixed $value): void |
public function __construct(
?\database\interface\IProvider $provider = null,
?\database\query\QueryBuilder $builder = null,
?\database\interface\ICache $cache = null,
)| Parameter | Type, modifiers and default | Purpose |
|---|---|---|
$provider |
?\database\interface\IProvider $provider = null |
Database execution collaborator; transactions require ITransactionalProvider. |
$builder |
?\database\query\QueryBuilder $builder = null |
SQL-building collaborator; mutable query state is local to this instance. |
$cache |
?\database\interface\ICache $cache = null |
Result-cache collaborator; reads/writes follow the owning manager policy. |
Computes or delegates the operation shown below; no explicit receiver property assignment or throw occurs in this method.
Implementation, validation guards and interaction order
public function __construct(
?\database\interface\IProvider $provider = null,
?\database\query\QueryBuilder $builder = null,
?\database\interface\ICache $cache = null,
) {
parent::__construct(
$provider ?? new \database\provider\DatabaseProvider(),
$builder ?? new \database\query\QueryBuilder(),
$cache ?? new \database\cache\DatabaseCache(),
);
}final public function selectByID(?int $id, string $table = '', string $column = ''): void| Parameter | Type, modifiers and default | Purpose |
|---|---|---|
$id |
?int $id |
The ID of the record to be selected. If null, the method returns without performing any operation. |
$table |
string $table = '' |
Optional. The name of the table to query from. Defaults to an inferred table name if not provided. |
$column |
string $column = '' |
Optional. The column name used to match the ID. Defaults to 'id' if not provided. |
Selects a record from the specified table by its ID and populates the object's properties with the data from the selected row.
@param int|null $id The ID of the record to be selected. If null, the method returns without performing any operation. @param string $table Optional. The name of the table to query from. Defaults to an inferred table name if not provided. @param string $column Optional. The column name used to match the ID. Defaults to 'id' if not provided.
@return void @throws ModelCreationException
State writes: $this->model_source, $this->id_column.
Explicit exceptions: ModelCreationException. The guards in the implementation below show when each is raised; exceptions from collaborators also propagate unless caught.
Failure handling catches Throwable.
Implementation, validation guards and interaction order
/**
* Selects a record from the specified table by its ID and populates the object's properties
* with the data from the selected row.
*
* @param int|null $id The ID of the record to be selected. If null, the method returns without performing any operation.
* @param string $table Optional. The name of the table to query from. Defaults to an inferred table name if not provided.
* @param string $column Optional. The column name used to match the ID. Defaults to 'id' if not provided.
*
* @return void
* @throws ModelCreationException
*/
final public function selectByID(?int $id, string $table = '', string $column = ''): void
{
if ($id === null) return;
$table = $table ?: ($this->model_source ?: $this->inferTableName());
$column = $column ?: ($this->id_column ?: 'id');
try {
$result = $this->table($table)->select()->where([$column => $id])->get(1);
$this->clearLoadedAttributes();
$this->model_source = $table;
$this->id_column = $column;
if ($result === []) return;
$this->hydrateRow($result[0], $table, $column);
} catch (Throwable $e) {
throw new ModelCreationException($e);
}
}public function inferTableName(): stringInfers the table name based on the class name of the current object. If the class name is in snake_case, it will be used directly. Otherwise, the class name will be converted from CamelCase to snake_case.
@return string The inferred table name in snake_case format.
Return expressions (including local callbacks): $this->isSnakeCase($shortClass) ? $shortClass : $this->camelToSnake($shortClass).
Implementation, validation guards and interaction order
/**
* Infers the table name based on the class name of the current object.
* If the class name is in snake_case, it will be used directly.
* Otherwise, the class name will be converted from CamelCase to snake_case.
*
* @return string The inferred table name in snake_case format.
*/
public function inferTableName(): string
{
$fullClass = get_class($this);
$exp = explode("\\", $fullClass);
$shortClass = end($exp);
return $this->isSnakeCase($shortClass) ? $shortClass : $this->camelToSnake($shortClass);
}public function loadWith(array $row): void| Parameter | Type, modifiers and default | Purpose |
|---|---|---|
$row |
array $row |
The associative array of data used to load the related object. |
Loads a related object based on the specified configuration and data row.
@param array $row The associative array of data used to load the related object. @return void
Delegates to with(), loadRelationObject().
Implementation, validation guards and interaction order
/**
* Loads a related object based on the specified configuration and data row.
*
* @param array $row The associative array of data used to load the related object.
* @return void
*/
public function loadWith(array $row): void
{
if (!($this instanceof IRelationWith)) return;
$with = $this->with();
if (empty($with['model']) || empty($with['column'])) return;
$this->loadRelationObject($with, $row);
}public function loadBelongsTo(array $row): void| Parameter | Type, modifiers and default | Purpose |
|---|---|---|
$row |
array $row |
An associative array representing the data row used to load the related object. |
Loads a "Belongs To" relationship object for the current model.
@param array $row An associative array representing the data row used to load the related object. @return void
Delegates to belongsTo(), loadRelationObject().
Implementation, validation guards and interaction order
/**
* Loads a "Belongs To" relationship object for the current model.
*
* @param array $row An associative array representing the data row used to load the related object.
* @return void
*/
public function loadBelongsTo(array $row): void
{
if (!($this instanceof IRelationBelongs)) return;
$belongs = $this->belongsTo();
if (empty($belongs['model']) || empty($belongs['column'])) return;
$this->loadRelationObject($belongs, $row);
}public function loadHasMany(array $row): void| Parameter | Type, modifiers and default | Purpose |
|---|---|---|
$row |
array $row |
The row of data representing the current model, containing values needed |
Loads and initializes "has many" relationship objects for the current model instance.
@param array $row The row of data representing the current model, containing values needed to establish relationships. @return void
State writes: $this->ormRelationKeys[].
Explicit exceptions: \InvalidArgumentException. The guards in the implementation below show when each is raised; exceptions from collaborators also propagate unless caught.
Implementation, validation guards and interaction order
/**
* Loads and initializes "has many" relationship objects for the current model instance.
*
* @param array $row The row of data representing the current model, containing values needed
* to establish relationships.
* @return void
*/
public function loadHasMany(array $row): void
{
if (!($this instanceof IRelationHasMany)) return;
$hasMany = $this->hasMany();
if (empty($hasMany['model']) || empty($hasMany['column'])) return;
$models = is_array($hasMany['model']) ? $hasMany['model'] : [$hasMany['model']];
$initialized = [];
foreach ($models as $model) {
$model = ltrim($model, '\\');
if (!is_subclass_of($model, self::class)) throw new \InvalidArgumentException('Related classes must extend Model.');
$shortName = $hasMany['as'] ?? (new ReflectionClass($model))->getShortName();
$this->ormRelationKeys[] = $shortName;
if (!isset($initialized[$shortName])) {
$this->{$shortName} = [];
$initialized[$shortName] = true;
}
$prototype = new $model(null);
$prototype->useConnectionFrom($this);
$table = $hasMany['table'] ?? $prototype->model_source ?? $prototype->inferTableName();
$key = $hasMany['key'] ?? $prototype->id_column ?? 'id';
$localKey = $hasMany['localKey'] ?? $this->id_column ?? 'id';
$localValue = $row[$localKey] ?? $this->model_id;
if ($localValue === null) continue;
$rows = $this->find($hasMany['column'], $localValue, $table);
foreach ($rows as $relatedRow) {
if (!empty($hasMany['inConstructor'])) {
$related = new $model($relatedRow[$key]);
$related->useConnectionFrom($this);
} else {
$related = clone $prototype;
$related->useConnectionFrom($this);
$related->hydrateRow($relatedRow, $table, $key);
}
$this->{$shortName}[] = $related;
}
}
}protected function loadRelationObject(array $relation, array $row): void| Parameter | Type, modifiers and default | Purpose |
|---|---|---|
$relation |
array $relation |
An associative array defining the relationship, containing details such as the model class and column for foreign key lookup. |
$row |
array $row |
The data row containing information necessary to resolve the relation, typically including the foreign key value. |
Loads and initializes a related object based on the provided relation and row data.
@param array $relation An associative array defining the relationship, containing details such as the model class and column for foreign key lookup. @param array $row The data row containing information necessary to resolve the relation, typically including the foreign key value. @return void
State writes: $this->ormRelationKeys[].
Explicit exceptions: \InvalidArgumentException. The guards in the implementation below show when each is raised; exceptions from collaborators also propagate unless caught.
Implementation, validation guards and interaction order
/**
* Loads and initializes a related object based on the provided relation and row data.
*
* @param array $relation An associative array defining the relationship, containing details such as the model class and column for foreign key lookup.
* @param array $row The data row containing information necessary to resolve the relation, typically including the foreign key value.
* @return void
*/
protected function loadRelationObject(array $relation, array $row): void
{
$modelClass = ltrim($relation['model'], '\\');
if (!is_subclass_of($modelClass, self::class)) throw new \InvalidArgumentException('Related classes must extend Model.');
$shortName = $relation['as'] ?? (new ReflectionClass($modelClass))->getShortName();
$this->ormRelationKeys[] = $shortName;
$this->{$shortName} = $this->createRelationObject($relation, $row);
}protected function createRelationObject(array $relation, array $row = []): ?object| Parameter | Type, modifiers and default | Purpose |
|---|---|---|
$relation |
array $relation |
An associative array detailing the relationship, including the model class and column for foreign key lookup. |
$row |
array $row = [] |
Optional associative array containing data for resolving the relation, typically with the foreign key value. |
Creates and returns a related object based on the provided relationship definition and optional row data.
@param array $relation An associative array detailing the relationship, including the model class and column for foreign key lookup. @param array $row Optional associative array containing data for resolving the relation, typically with the foreign key value. @return object|null The created related object if successful, or null if the related model class does not exist.
Return expressions (including local callbacks): null; (new $modelClass($foreignValue))->useConnectionFrom($this); $related->existsInDatabase() ? $related : null.
Explicit exceptions: \InvalidArgumentException. The guards in the implementation below show when each is raised; exceptions from collaborators also propagate unless caught.
Implementation, validation guards and interaction order
/**
* Creates and returns a related object based on the provided relationship definition and optional row data.
*
* @param array $relation An associative array detailing the relationship, including the model class and column for foreign key lookup.
* @param array $row Optional associative array containing data for resolving the relation, typically with the foreign key value.
* @return object|null The created related object if successful, or null if the related model class does not exist.
*/
protected function createRelationObject(array $relation, array $row = []): ?object
{
$modelClass = ltrim($relation['model'], '\\');
if (!is_subclass_of($modelClass, self::class)) throw new \InvalidArgumentException('Related classes must extend Model.');
$foreignValue = $row[$relation['column']] ?? null;
if ($foreignValue === null) return null;
if (!empty($relation['inConstructor'])) {
return (new $modelClass($foreignValue))->useConnectionFrom($this);
}
$related = (new $modelClass())->useConnectionFrom($this);
$related->selectByID($foreignValue, $relation['table'] ?? '', $relation['key'] ?? '');
return $related->existsInDatabase() ? $related : null;
}final protected function selectAll(string $table = ''): array| Parameter | Type, modifiers and default | Purpose |
|---|---|---|
$table |
string $table = '' |
The name of the table to select from. |
Retrieves all records from the specified table. If no table is provided, it infers the table name from the class name. This method does not populate any properties, as it is intended for fetching data without direct assignment.
@param string $table The name of the table to select from. @return array Returns the array of query result;
Return expressions (including local callbacks): $this->table($table)->select()->get().
Implementation, validation guards and interaction order
/**
* Retrieves all records from the specified table. If no table
* is provided, it infers the table name from the class name.
* This method does not populate any properties, as it is
* intended for fetching data without direct assignment.
*
* @param string $table The name of the table to select from.
* @return array Returns the array of query result;
*/
final protected function selectAll(string $table = ''): array
{
$table = $table ?: ($this->model_source ?: $this->inferTableName());
return $this->table($table)->select()->get();
}final public function getParam(string $key): mixed| Parameter | Type, modifiers and default | Purpose |
|---|---|---|
$key |
string $key |
The name of the property to retrieve. |
Returns the value of the specified property if it exists in the model; otherwise, it returns null. This method is useful for accessing model properties dynamically.
@param string $key The name of the property to retrieve. @return mixed The value of the property or null if it does not exist.
Return expressions (including local callbacks): $property->isPublic() && $property->isInitialized($this) ? $this->{$key} : null; $this->dynamicData[$key] ?? null.
Implementation, validation guards and interaction order
/**
* Returns the value of the specified property if it exists in
* the model; otherwise, it returns null. This method is useful
* for accessing model properties dynamically.
*
* @param string $key The name of the property to retrieve.
* @return mixed The value of the property or null if it does not exist.
*/
final public function getParam(string $key): mixed
{
if (property_exists($this, $key)) {
$property = new ReflectionProperty($this, $key);
return $property->isPublic() && $property->isInitialized($this) ? $this->{$key} : null;
}
return $this->dynamicData[$key] ?? null;
}public function setModelId(int $id): void| Parameter | Type, modifiers and default | Purpose |
|---|---|---|
$id |
int $id |
Record or runtime identifier selected by this operation. |
State writes: $this->model_id.
Implementation, validation guards and interaction order
public function setModelId(int $id): void
{
$this->model_id = $id;
}public function save(): boolSaves the current state of the model to the database by updating the record associated with the model's identifier column.
The method converts the model's properties into an array, applies necessary transformations (such as setting an updated timestamp, if applicable), and removes blacklisted parameters from the update data. It then performs an update operation on the corresponding database table.
@return bool True if the update operation was successful, false otherwise. @throws ModelCRUDException
State writes: $this->model_source, $this->id_column, $this->ormLastChanges.
Return expressions (including local callbacks): false; $result.
Explicit exceptions: \LogicException, ModelCRUDException. The guards in the implementation below show when each is raised; exceptions from collaborators also propagate unless caught.
Failure handling catches Throwable.
Implementation, validation guards and interaction order
/**
* Saves the current state of the model to the database by updating the record
* associated with the model's identifier column.
*
* The method converts the model's properties into an array, applies necessary
* transformations (such as setting an updated timestamp, if applicable), and
* removes blacklisted parameters from the update data. It then performs an
* update operation on the corresponding database table.
*
* @return bool True if the update operation was successful, false otherwise.
* @throws ModelCRUDException
*/
public function save(): bool
{
try {
if ($this->model_id === null) throw new \LogicException('Cannot update a model without a primary key.');
$table = $this->model_source ?: $this->inferTableName();
$column = $this->id_column ?: 'id';
$update = $this->persistenceAttributes();
unset($update[$column]);
if ($this->checkColumn($table, 'updated_on')) {
$update['updated_on'] = new \database\query\SqlExpression('CURRENT_TIMESTAMP');
}
if ($update === []) return false;
$changes = $this->getDirty();
foreach ($update as $attribute => $value) {
if ($value === 'DEFAULT') $update[$attribute] = new \database\query\BoundValue($value);
}
$result = $this->table($table)->where([$column => $this->model_id])->update($update);
if ($result) {
$this->model_source = $table;
$this->id_column = $column;
$this->ormLastChanges = $changes;
$this->syncOriginal();
}
return $result;
} catch (Throwable $e) {
throw new ModelCRUDException($e);
}
}public function saveModel(): int|nullSaves the current model to the database by converting it to an array, removing blacklisted parameters, and inserting it into the specified table.
@return bool True if the model was successfully saved, false otherwise. @throws ModelCRUDException
State writes: $this->model_source, $this->id_column, $this->model_id, $this->ormLastChanges.
Return expressions (including local callbacks): $id.
Explicit exceptions: ModelCRUDException. The guards in the implementation below show when each is raised; exceptions from collaborators also propagate unless caught.
Failure handling catches Throwable.
Implementation, validation guards and interaction order
/**
* Saves the current model to the database by converting it to an array,
* removing blacklisted parameters, and inserting it into the specified table.
*
* @return bool True if the model was successfully saved, false otherwise.
* @throws ModelCRUDException
*/
public function saveModel(): int|null
{
try {
$this->model_source ??= $this->inferTableName();
$this->id_column ??= 'id';
$save = $this->persistenceAttributes();
$id = $this->table($this->model_source)->insert($save)->executeInsert();
if ($id !== null) {
$this->model_id = $id;
$this->assignAttribute($this->id_column, $id);
$this->ormLastChanges = $save;
$this->syncOriginal();
}
return $id;
} catch (Throwable $e) {
throw new ModelCRUDException($e);
}
}public function deleteModel(): boolDeletes the current model from the data source (database) based on the defined identifier column and its value. The identifier column is determined by $id_column, defaulting to "id" if not set.
@return bool True if the model was successfully deleted, false otherwise. @throws ModelCRUDException
State writes: $this->id_column, $this->model_source, $this->model_id.
Return expressions (including local callbacks): $deleted.
Explicit exceptions: \LogicException, ModelCRUDException. The guards in the implementation below show when each is raised; exceptions from collaborators also propagate unless caught.
Failure handling catches Throwable.
Implementation, validation guards and interaction order
/**
* Deletes the current model from the data source (database) based on the defined identifier column and its value.
* The identifier column is determined by $id_column, defaulting to "id" if not set.
*
* @return bool True if the model was successfully deleted, false otherwise.
* @throws ModelCRUDException
*/
public function deleteModel(): bool
{
if ($this->id_column === null)
$this->id_column = "id";
$where = [$this->id_column => $this->model_id];
if($this->model_source === null)
$this->model_source = $this->inferTableName();
try {
if ($this->model_id === null) throw new \LogicException('Cannot delete a model without a primary key.');
$deleted = $this->table($this->model_source)->where($where)->delete();
if ($deleted) $this->model_id = null;
return $deleted;
} catch (Throwable $e) {
throw new ModelCRUDException($e);
}
}public function find(string $column, mixed $value, ?string $source = null): array| Parameter | Type, modifiers and default | Purpose |
|---|---|---|
$column |
string $column |
Operation input; the exact use, validation and propagation are shown in the implementation trace. |
$value |
mixed $value |
Operation input; the exact use, validation and propagation are shown in the implementation trace. |
$source |
?string $source = null |
Operation input; the exact use, validation and propagation are shown in the implementation trace. |
@throws ModelCRUDException
Return expressions (including local callbacks): $this->table($source)->select()->where([$column => $value])->get().
Explicit exceptions: ModelCRUDException. The guards in the implementation below show when each is raised; exceptions from collaborators also propagate unless caught.
Failure handling catches Throwable.
Implementation, validation guards and interaction order
/**
* @throws ModelCRUDException
*/
public function find(string $column, mixed $value, ?string $source = null): array
{
$source ??= $this->model_source ?: $this->inferTableName();
try {
return $this->table($source)->select()->where([$column => $value])->get();
} catch (Throwable $e) {
throw new ModelCRUDException($e);
}
}public function paramBlackList(string $key): void| Parameter | Type, modifiers and default | Purpose |
|---|---|---|
$key |
string $key |
The key to be added to the parameter blacklist. |
Adds a given key to the blacklist of parameters.
@param string $key The key to be added to the parameter blacklist. @return void
State writes: $this->paramBlackList[].
Implementation, validation guards and interaction order
/**
* Adds a given key to the blacklist of parameters.
*
* @param string $key The key to be added to the parameter blacklist.
* @return void
*/
public function paramBlackList(string $key): void
{
$this->paramBlackList[] = $key;
}protected function camelToSnake(string $input): string| Parameter | Type, modifiers and default | Purpose |
|---|---|---|
$input |
string $input |
string for conversion |
Converts a given string to snake case.
@param string $input string for conversion @return string
Return expressions (including local callbacks): $input; strtolower($snake).
Implementation, validation guards and interaction order
/**
* Converts a given string to snake case.
*
* @param string $input string for conversion
* @return string
*/
protected function camelToSnake(string $input): string
{
if ($this->isSnakeCase($input)) {
return $input;
}
$snake = preg_replace('/(?<!^)[A-Z]/', '_$0', $input);
return strtolower($snake);
}private function isSnakeCase(string $input): bool| Parameter | Type, modifiers and default | Purpose |
|---|---|---|
$input |
string $input |
* @return bool |
Helper function to determine if classname is snake cased @param string $input @return bool
Return expressions (including local callbacks): (bool)preg_match('/^[a-z0-9]+(?:_[a-z0-9]+)*$/', $input).
Implementation, validation guards and interaction order
/**
* Helper function to determine if classname is snake cased
* @param string $input
* @return bool
*/
private function isSnakeCase(string $input): bool
{
return (bool)preg_match('/^[a-z0-9]+(?:_[a-z0-9]+)*$/', $input);
}public function fromArray(array $data): void| Parameter | Type, modifiers and default | Purpose |
|---|---|---|
$data |
array $data |
Input data array or payload; see allowed fields and validation guards below. |
Legacy trusted-data assignment. Use fill() for request input.
Delegates to assignAttribute().
Implementation, validation guards and interaction order
/** Legacy trusted-data assignment. Use fill() for request input. */
public function fromArray(array $data): void
{
foreach ($data as $key => $value) $this->assignAttribute((string) $key, $value);
}public function fill(array $data): static| Parameter | Type, modifiers and default | Purpose |
|---|---|---|
$data |
array $data |
Input data array or payload; see allowed fields and validation guards below. |
Only explicitly allowlisted attributes may be filled from untrusted data.
Return expressions (including local callbacks): $this.
Implementation, validation guards and interaction order
/** Only explicitly allowlisted attributes may be filled from untrusted data. */
public function fill(array $data): static
{
$allowed = array_flip($this->fillableAttributes());
foreach ($data as $key => $value) {
if (isset($allowed[$key]) && !in_array($key, ['model_source', 'id_column', 'tables', 'dynamicData'], true)) {
$this->assignAttribute((string) $key, $value);
}
}
return $this;
}protected function fillableAttributes(): arrayReturn expressions (including local callbacks): [].
Implementation, validation guards and interaction order
protected function fillableAttributes(): array { return []; }protected function hiddenAttributes(): arrayReturn expressions (including local callbacks): [].
Implementation, validation guards and interaction order
protected function hiddenAttributes(): array { return []; }protected function attributeCasts(): arrayReturn expressions (including local callbacks): [].
Implementation, validation guards and interaction order
protected function attributeCasts(): array { return []; }protected function writableAttributes(): ?arraynull preserves the legacy blacklist-based persistence contract.
Return expressions (including local callbacks): null.
Implementation, validation guards and interaction order
/** null preserves the legacy blacklist-based persistence contract. */
protected function writableAttributes(): ?array { return null; }private function assignAttribute(string $key, mixed $value): void| Parameter | Type, modifiers and default | Purpose |
|---|---|---|
$key |
string $key |
Operation input; the exact use, validation and propagation are shown in the implementation trace. |
$value |
mixed $value |
Operation input; the exact use, validation and propagation are shown in the implementation trace. |
State writes: $this->dynamicData[$key].
Explicit exceptions: \InvalidArgumentException. The guards in the implementation below show when each is raised; exceptions from collaborators also propagate unless caught.
Implementation, validation guards and interaction order
private function assignAttribute(string $key, mixed $value): void
{
if (property_exists($this, $key)) {
$property = new ReflectionProperty($this, $key);
if (!$property->isPublic() || $property->isStatic()) {
throw new \InvalidArgumentException("Cannot assign internal model property: {$key}");
}
}
$cast = $this->attributeCasts()[$key] ?? null;
if ($value !== null && $cast !== null) {
$value = match ($cast) {
'int', 'integer' => (int) $value,
'float' => (float) $value,
'string' => (string) $value,
'bool', 'boolean' => filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE)
?? throw new \InvalidArgumentException("Invalid boolean attribute: {$key}"),
'json', 'array' => is_string($value) ? json_decode($value, true, 512, JSON_THROW_ON_ERROR) : $value,
default => throw new \InvalidArgumentException("Unsupported attribute cast: {$cast}"),
};
}
unset($this->ormMissingAttributes[$key]);
if (property_exists($this, $key)) $this->{$key} = $value;
else $this->dynamicData[$key] = $value;
}public function hydrateRow(array $row, ?string $table = null, ?string $key = null): static| Parameter | Type, modifiers and default | Purpose |
|---|---|---|
$row |
array $row |
Operation input; the exact use, validation and propagation are shown in the implementation trace. |
$table |
?string $table = null |
Operation input; the exact use, validation and propagation are shown in the implementation trace. |
$key |
?string $key = null |
Operation input; the exact use, validation and propagation are shown in the implementation trace. |
Hydrate an already fetched row without executing a constructor lookup.
State writes: $this->ormLoadedKeys, $this->model_source, $this->id_column, $this->model_id.
Return expressions (including local callbacks): $this.
Implementation, validation guards and interaction order
/** Hydrate an already fetched row without executing a constructor lookup. */
public function hydrateRow(array $row, ?string $table = null, ?string $key = null): static
{
$this->clearLoadedAttributes();
$this->fromArray($row);
$this->ormLoadedKeys = array_keys($row);
$this->model_source = $table ?? $this->model_source ?? $this->inferTableName();
$this->id_column = $key ?? $this->id_column ?? 'id';
$this->model_id = isset($row[$this->id_column]) ? (int) $row[$this->id_column] : null;
$identity = static::class . ':' . $this->model_source . ':' . $this->id_column . ':' . $this->model_id;
if (!isset(self::$ormLoading[$identity])) {
self::$ormLoading[$identity] = true;
try {
$this->loadWith($row);
$this->loadBelongsTo($row);
$this->loadHasMany($row);
} finally { unset(self::$ormLoading[$identity]); }
}
$this->syncOriginal();
return $this;
}private function clearLoadedAttributes(): voidState writes: $this->dynamicData, $this->model_id, $this->ormOriginal, $this->ormRelationKeys, $this->ormLastChanges, $this->ormMissingAttributes, $this->ormLoadedKeys.
Implementation, validation guards and interaction order
private function clearLoadedAttributes(): void
{
$defaults = (new ReflectionClass($this))->getDefaultProperties();
foreach (array_unique([...$this->ormLoadedKeys, ...array_keys($this->ormOriginal)]) as $key) {
if (in_array($key, ['model_source', 'id_column', 'tables', 'dynamicData'], true)) continue;
if (property_exists($this, $key)) {
$property = new ReflectionProperty($this, $key);
if (!$property->isPublic() || $property->isReadOnly()) continue;
if (array_key_exists($key, $defaults)) $this->{$key} = $defaults[$key];
elseif ($property->getType()?->allowsNull()) $this->{$key} = null;
else unset($this->{$key});
}
}
$this->dynamicData = [];
$this->model_id = null;
$this->ormOriginal = [];
foreach ($this->ormRelationKeys as $key) unset($this->{$key});
$this->ormRelationKeys = [];
$this->ormLastChanges = [];
$this->ormMissingAttributes = [];
$this->ormLoadedKeys = [];
}private function persistenceAttributes(): arrayReturn expressions (including local callbacks): $attributes.
Implementation, validation guards and interaction order
private function persistenceAttributes(): array
{
$attributes = array_diff_key($this->__toArray(), array_flip([
...$this->paramBlackList, 'tables', 'model_source', 'id_column', ...$this->ormRelationKeys,
]));
$writable = $this->writableAttributes();
if ($writable !== null) $attributes = array_intersect_key($attributes, array_flip($writable));
foreach ($attributes as $key => $value) {
$cast = $this->attributeCasts()[$key] ?? null;
if ($value !== null && in_array($cast, ['json', 'array'], true)) {
$attributes[$key] = json_encode($value, JSON_THROW_ON_ERROR);
} elseif ($value instanceof \BackedEnum) $attributes[$key] = $value->value;
}
return $attributes;
}public function newQuery(): staticStart an independent SELECT using this model's configured table and connection.
Return expressions (including local callbacks): $query.
Implementation, validation guards and interaction order
/** Start an independent SELECT using this model's configured table and connection. */
public function newQuery(): static
{
$query = clone $this;
$query->useConnectionFrom($this);
$query->clearLoadedAttributes();
$query->table($this->model_source ?: $this->inferTableName())->select();
return $query;
}public function getModels(?int $limit = null, ?int $offset = null): array| Parameter | Type, modifiers and default | Purpose |
|---|---|---|
$limit |
?int $limit = null |
Maximum result count; query pagination rejects negative values. |
$offset |
?int $offset = null |
Skipped row count; SQL offset is emitted only when a limit is set. |
Hydrated results from the current fluent query; legacy get() still returns rows.
Return expressions (including local callbacks): array_map(function (array $row) use ($table, $key) { $model = clone $this; $model->hydrateRow($row, $table, $key).
Implementation, validation guards and interaction order
/** Hydrated results from the current fluent query; legacy get() still returns rows. */
public function getModels(?int $limit = null, ?int $offset = null): array
{
$table = $this->queryTable() ?: ($this->model_source ?: $this->inferTableName());
$key = $this->id_column ?: 'id';
return array_map(function (array $row) use ($table, $key) {
$model = clone $this;
$model->useConnectionFrom($this);
return $model->hydrateRow($row, $table, $key);
}, $this->get($limit, $offset));
}public function firstModel(): ?staticReturn expressions (including local callbacks): $this->getModels(1)[0] ?? null.
Implementation, validation guards and interaction order
public function firstModel(): ?static { return $this->getModels(1)[0] ?? null; }public function firstModelOrFail(): staticReturn expressions (including local callbacks): $this->firstModel() ?? throw new \OutOfBoundsException('Model record not found.').
Explicit exceptions: \OutOfBoundsException. The guards in the implementation below show when each is raised; exceptions from collaborators also propagate unless caught.
Implementation, validation guards and interaction order
public function firstModelOrFail(): static
{
return $this->firstModel() ?? throw new \OutOfBoundsException('Model record not found.');
}public function syncOriginal(): staticState writes: $this->ormOriginal.
Return expressions (including local callbacks): $this.
Implementation, validation guards and interaction order
public function syncOriginal(): static { $this->ormOriginal = $this->persistenceAttributes(); return $this; }public function getDirty(): arrayReturn expressions (including local callbacks): array_filter($this->persistenceAttributes(), fn($value, $key) => !array_key_exists($key, $this->ormOriginal) || $this->ormOriginal[$key] !== $value, ARRAY_FILTER_USE_BOTH).
Implementation, validation guards and interaction order
public function getDirty(): array
{
return array_filter($this->persistenceAttributes(), fn($value, $key) =>
!array_key_exists($key, $this->ormOriginal) || $this->ormOriginal[$key] !== $value, ARRAY_FILTER_USE_BOTH);
}public function isDirty(?string $attribute = null): bool| Parameter | Type, modifiers and default | Purpose |
|---|---|---|
$attribute |
?string $attribute = null |
Operation input; the exact use, validation and propagation are shown in the implementation trace. |
Return expressions (including local callbacks): $attribute === null ? $dirty !== [] : array_key_exists($attribute, $dirty).
Implementation, validation guards and interaction order
public function isDirty(?string $attribute = null): bool
{
$dirty = $this->getDirty();
return $attribute === null ? $dirty !== [] : array_key_exists($attribute, $dirty);
}public function getChanges(): arrayReturn expressions (including local callbacks): $this->ormLastChanges.
Implementation, validation guards and interaction order
public function getChanges(): array { return $this->ormLastChanges; }public function existsInDatabase(): boolReturn expressions (including local callbacks): $this->model_id !== null.
Implementation, validation guards and interaction order
public function existsInDatabase(): bool { return $this->model_id !== null; }public function getKey(): ?intReturn expressions (including local callbacks): $this->model_id.
Implementation, validation guards and interaction order
public function getKey(): ?int { return $this->model_id; }public function toArray(): arrayReturn expressions (including local callbacks): array_diff_key($this->__toArray(), array_flip(['tables', 'model_source', 'id_column', ...$this->hiddenAttributes()])).
Implementation, validation guards and interaction order
public function toArray(): array
{
return array_diff_key($this->__toArray(), array_flip(['tables', 'model_source', 'id_column', ...$this->hiddenAttributes()]));
}public function jsonSerialize(): arrayReturn expressions (including local callbacks): $this->toArray().
Implementation, validation guards and interaction order
public function jsonSerialize(): array { return $this->toArray(); }public function __toString(): stringGenerates a string representation of the object by creating a JSON-encoded string of its public properties and their values.
@return string A JSON-formatted string representing the public properties of the object.
Return expressions (including local callbacks): json_encode($this->toArray(), JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES).
Implementation, validation guards and interaction order
/**
* Generates a string representation of the object by creating a JSON-encoded
* string of its public properties and their values.
*
* @return string A JSON-formatted string representing the public properties of the object.
*/
public function __toString(): string
{
return json_encode($this->toArray(), JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
}public function __toArray(): arrayConverts the object's public properties into an associative array. Only includes properties that are publicly accessible.
@return array Returns an associative array of the object's public properties.
Return expressions (including local callbacks): $result.
Implementation, validation guards and interaction order
/**
* Converts the object's public properties into an associative array.
* Only includes properties that are publicly accessible.
*
* @return array Returns an associative array of the object's public properties.
*/
public function __toArray(): array
{
$reflect = new ReflectionClass($this);
$vars = $reflect->getProperties(ReflectionProperty::IS_PUBLIC);
$result = [];
foreach ($vars as $property) {
if (!$property->isStatic() && $property->isInitialized($this)) {
$result[$property->getName()] = $property->getValue($this);
}
}
foreach($this->dynamicData as $key => $value) {
if ($value === null && isset($this->ormMissingAttributes[$key])) continue;
$result[$key] = $value;
}
unset($result['dynamicData']);
return $result;
}public function __isset(string $name): bool| Parameter | Type, modifiers and default | Purpose |
|---|---|---|
$name |
string $name |
Name of the property to retrieve. |
Retrieves the value of a requested property. Checks for both declared properties and dynamic data.
@param string $name Name of the property to retrieve. @return mixed Returns the value of the property if it exists, or null if not found.
Return expressions (including local callbacks): $property->isPublic() && $property->isInitialized($this) && $this->{$name} !== null; isset($this->dynamicData[$name]).
Implementation, validation guards and interaction order
/**
* Retrieves the value of a requested property.
* Checks for both declared properties and dynamic data.
*
* @param string $name Name of the property to retrieve.
* @return mixed Returns the value of the property if it exists, or null if not found.
*/
public function __isset(string $name): bool
{
if (property_exists($this, $name)) {
$property = new ReflectionProperty($this, $name);
return $property->isPublic() && $property->isInitialized($this) && $this->{$name} !== null;
}
return isset($this->dynamicData[$name]);
}public function &__get(string $name): mixed| Parameter | Type, modifiers and default | Purpose |
|---|---|---|
$name |
string $name |
Named key/label selected by this API; its exact scope is shown by the implementation trace. |
State writes: $this->ormRelationKeys[], $this->dynamicData[$name], $this->ormMissingAttributes[$name].
Return expressions (including local callbacks): $this->dynamicData[$name].
Explicit exceptions: \LogicException. The guards in the implementation below show when each is raised; exceptions from collaborators also propagate unless caught.
Implementation, validation guards and interaction order
public function &__get(string $name): mixed
{
if (property_exists($this, $name)) {
throw new \LogicException("Model property is internal or uninitialized: {$name}");
}
if (array_key_exists($name, $this->dynamicData)) {
return $this->dynamicData[$name];
}
if ($this instanceof IRelationWith) {
$with = $this->with();
$modelClass = $with['model'] ?? null;
if ($modelClass) {
$parts = explode('\\', $modelClass);
$shortName = end($parts);
$alias = $with['as'] ?? $shortName;
if ($alias === $name) {
$this->ormRelationKeys[] = $name;
$this->dynamicData[$name] = $this->createRelationObject($with, $this->__toArray());
return $this->dynamicData[$name];
}
}
}
// Initialize null if nothing found
$this->ormMissingAttributes[$name] = true;
$this->dynamicData[$name] = null;
return $this->dynamicData[$name];
}public function __set(string $name, mixed $value): void| Parameter | Type, modifiers and default | Purpose |
|---|---|---|
$name |
string $name |
The name of the property to set. |
$value |
mixed $value |
The value to assign to the property. |
Dynamically sets a value to a property. Updates an existing property if it exists, otherwise adds it to dynamic data.
@param string $name The name of the property to set. @param mixed $value The value to assign to the property.
@return void
Delegates to assignAttribute().
Implementation, validation guards and interaction order
/**
* Dynamically sets a value to a property.
* Updates an existing property if it exists, otherwise adds it to dynamic data.
*
* @param string $name The name of the property to set.
* @param mixed $value The value to assign to the property.
*
* @return void
*/
public function __set(string $name, mixed $value): void
{
$this->assignAttribute($name, $value);
}AWT Framework Wiki · Home · Installation Guide · Developer Guide · Framework Internals
Philosophy · Architecture · Known discrepancies · Verification
Installation Guide
Developer Guide
- Learning sequence
- First package
- Package structure
- Lifecycle
- Runtimes
- Events and objects
- Routing and controllers
- Views and resources
- Database and models
- Storage and caching
- Configuration and CLI
- Responses and errors
- Extension points
Framework Internals
- Architecture
- Bootstrap
- Packages
- Runtime
- Events and context
- Objects
- HTTP routing and controllers
- Rendering
- Blade integration
- Database
- Models
- Storage and caching
- Settings
- CLI
- Identity and sessions
- Responses
- Errors
- Complete source inventory
Documentation notes