VELOX API Docs

Element extends DBAL
in package
implements ArrayAccess, Traversable, IteratorAggregate

An abstract class that holds the base functionality of a model.

NOTE: This class is not meant to be used directly.

Tags
since
1.5.1

Interfaces, Classes and Traits

ArrayAccess
Traversable
IteratorAggregate

Table of Contents

$attributes  : array<string|int, mixed>
Model attributes. Corresponds to table columns.
$columns  : array<string|int, mixed>|null
Model table columns. If not set, the model will fall back to the default primary key `['id']`.
$database  : Database|null
The database instance/connection.
$primaryKey  : string|null
Model table primary key. If not set, `id` will be used by default.
$table  : string|null
Model table name. If not set, an auto-generated name will be used instead.
__clone()  : mixed
Makes the model safely cloneable via the `clone` keyword.
__construct()  : mixed
Class constructor.
__get()  : mixed
Makes attributes accessible via public property access notation.
__isset()  : mixed
Makes attributes consumable via `isset()`.
__set()  : mixed
Makes attributes accessible via public property assignment notation.
__sleep()  : mixed
Makes the model safely consumable via `serialize()`.
__unset()  : mixed
Makes attributes consumable via `unset()`.
fetch()  : array<string|int, static>|array<string|int, array<string|int, mixed>>
Executes a query (a prepared statement) and returns the result.
get()  : mixed
Gets the specified model attribute.
getAttributes()  : array<string|int, mixed>
Gets all model attributes.
getColumns()  : array<string|int, mixed>
Returns model table columns and sets `static::$columns` with a default value and returns it if it's not set.
getDatabase()  : Database
Returns model database connection and sets `static::$database` with a default value if it's not set.
getIterator()  : Traversable
`IteratorAggregate::getIterator()` interface implementation.
getPrimaryKey()  : string
Returns model table primary key and sets `static::$primaryKey` with a default value and returns it if it's not set.
getTable()  : string
Returns model table name and sets `static::$table` with a default value and returns it if it's not set.
isMigrated()  : bool
Checks whether the model table is migrated to the database or not.
migrate()  : void
Migrates model table to the database.
offsetExists()  : bool
`ArrayAccess::offsetExists()` interface implementation.
offsetGet()  : mixed
`ArrayAccess::offsetGet()` interface implementation.
offsetSet()  : void
`ArrayAccess::offsetSet()` interface implementation.
offsetUnset()  : void
`ArrayAccess::offsetUnset()` interface implementation.
schema()  : string
The SQL code to create the model table from. Has to match `self::$table`, `self::$columns`, and `self::$primaryKey`.
set()  : $this
Sets the specified model attribute.
setAttributes()  : $this
Sets all or a subset of model attributes.
where()  : array<string|int, static>|array<string|int, array<string|int, mixed>>
Finds a single or multiple models by the passed condition.
assertAttributeExists()  : void
Asserts that the model attribute name is valid.
bootstrap()  : void
Override this method to add your own bootstrap code to the modal.
buildNestedQuery()  : array<string|int, mixed>
Builds a nested query.
buildWhereClause()  : array<string|int, mixed>
validateCondition()  : array<string|int, mixed>
Validates the passed condition.
validateOperator()  : string
Validates the passed operator.

Properties

$attributes

Model attributes. Corresponds to table columns.

protected array<string|int, mixed> $attributes

$columns

Model table columns. If not set, the model will fall back to the default primary key `['id']`.

protected static array<string|int, mixed>|null $columns = ['id']

For good practice, keep the table columns in snake_case. Model attribute names match table columns.

$database

The database instance/connection.

protected static Database|null $database

$primaryKey

Model table primary key. If not set, `id` will be used by default.

protected static string|null $primaryKey = 'id'

$table

Model table name. If not set, an auto-generated name will be used instead.

protected static string|null $table = null

For good practice, keep the model name in singular form and make the table name in plural form.

Methods

__clone()

Makes the model safely cloneable via the `clone` keyword.

public __clone() : mixed
Return values
mixed

__construct()

Class constructor.

public __construct([array<string|int, mixed> $attributes = [] ]) : mixed

Keep all constructor arguments optional when extending the class. Or use self::bootstrap() instead.

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

[optional] The attributes to set on the model.

Return values
mixed

__get()

Makes attributes accessible via public property access notation.

public __get(string $name) : mixed

Examples: model_id as $model->modelId

Parameters
$name : string
Return values
mixed

__isset()

Makes attributes consumable via `isset()`.

public __isset(string $name) : mixed
Parameters
$name : string
Return values
mixed

__set()

Makes attributes accessible via public property assignment notation.

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

Examples: model_id as $model->modelId

Parameters
$name : string
$value : mixed
Return values
mixed

__sleep()

Makes the model safely consumable via `serialize()`.

public __sleep() : mixed
Return values
mixed

__unset()

Makes attributes consumable via `unset()`.

public __unset(string $name) : mixed
Parameters
$name : string
Return values
mixed

fetch()

Executes a query (a prepared statement) and returns the result.

public static fetch(string $query[, array<string|int, mixed>|null $variables = [] ][, bool $raw = false ]) : array<string|int, static>|array<string|int, array<string|int, mixed>>
Parameters
$query : string

The query to execute. The @table can be used to inject the current model table name into the query.

$variables : array<string|int, mixed>|null = []

[optional] The variables needed for the query.

$raw : bool = false

[optional] Whether fetch the models as arrays (raw) or as hydrated objects.

Tags
throws
BadMethodCallException

If called in an abstract class context.

Example:

  • Model::fetch('SELECT * FROM `users` WHERE `name` = :name OR `age` = :age', ['name' => 'Doe', 'age' => 27], true)
Return values
array<string|int, static>|array<string|int, array<string|int, mixed>>

The result as an array of objects or array of arrays depending on the passed parameters.

get()

Gets the specified model attribute.

public get(string $name) : mixed
Parameters
$name : string

Attribute name as specified in $columns.

Tags
throws
OutOfBoundsException

If the attribute does not exists.

Return values
mixed

Attribute value.

getAttributes()

Gets all model attributes.

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

Model attributes.

getColumns()

Returns model table columns and sets `static::$columns` with a default value and returns it if it's not set.

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

getDatabase()

Returns model database connection and sets `static::$database` with a default value if it's not set.

public static getDatabase() : Database
Return values
Database

getIterator()

`IteratorAggregate::getIterator()` interface implementation.

public getIterator() : Traversable
Return values
Traversable

getPrimaryKey()

Returns model table primary key and sets `static::$primaryKey` with a default value and returns it if it's not set.

public static getPrimaryKey() : string
Return values
string

getTable()

Returns model table name and sets `static::$table` with a default value and returns it if it's not set.

public static getTable() : string
Return values
string

isMigrated()

Checks whether the model table is migrated to the database or not.

public static isMigrated() : bool

You can override this method and return always true to disable auto migration.

NOTE: For compatibility reasons, the return value of this method true if it fails to connect to the database.

Return values
bool

migrate()

Migrates model table to the database.

public final static migrate() : void
Tags
throws
BadMethodCallException

If called in an abstract class context.

Return values
void

offsetExists()

`ArrayAccess::offsetExists()` interface implementation.

public offsetExists(mixed $offset) : bool
Parameters
$offset : mixed
Return values
bool

offsetGet()

`ArrayAccess::offsetGet()` interface implementation.

public offsetGet(mixed $offset) : mixed
Parameters
$offset : mixed
Return values
mixed

offsetSet()

`ArrayAccess::offsetSet()` interface implementation.

public offsetSet(mixed $offset, mixed $value) : void
Parameters
$offset : mixed
$value : mixed
Return values
void

offsetUnset()

`ArrayAccess::offsetUnset()` interface implementation.

public offsetUnset(mixed $offset) : void
Parameters
$offset : mixed
Return values
void

schema()

The SQL code to create the model table from. Has to match `self::$table`, `self::$columns`, and `self::$primaryKey`.

public abstract static schema() : string

Example: CREATE TABLE IF NOT EXISTS `table` (`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY, `text` VARCHAR(255));

Return values
string

set()

Sets the specified model attribute.

public set(string $name, mixed $value) : $this
Parameters
$name : string

Attribute name as specified in $columns.

$value : mixed

Attribute value.

Tags
throws
OutOfBoundsException

If the attribute does not exists.

Return values
$this

setAttributes()

Sets all or a subset of model attributes.

public setAttributes(array<string|int, mixed> $attributes) : $this
Parameters
$attributes : array<string|int, mixed>

Model attributes.

Return values
$this

where()

Finds a single or multiple models by the passed condition.

public static where(string $column, string $operator, mixed $value[, array<string|int, array<string|int, mixed>> $additional = null ][, string|null $order = null ][, int|null $limit = null ][, int|null $offset = null ]) : array<string|int, static>|array<string|int, array<string|int, mixed>>
Parameters
$column : string

The column/attribute name.

$operator : string

Condition operator, can be: =, !=, <>, <, >, <=, >=, LIKE, NOT LIKE, IN, NOT IN.

$value : mixed

The value to compare to.

$additional : array<string|int, array<string|int, mixed>> = null

[optional] Additional conditions. Can be used to add more conditions to the WHERE clause. Deep nesting can be achieved by simply using a child array.

$order : string|null = null

[optional] SQL order expression (like: id or id ASC).

$limit : int|null = null

[optional] To how many items the result should be limited.

$offset : int|null = null

[optional] From which item the result should start.

Tags
throws
InvalidArgumentException

If operator is not supported or a condition is invalid.

throws
BadMethodCallException

If called in an abstract class context.

Examples:

  • Model::where('name', '=', 'Doe').
  • Model::where('age', '>', 27, [['AND', 'name', 'LIKE', 'Doe%'], $query, ..., [$subQuery, ...]], $order, $limit, $offset).
Return values
array<string|int, static>|array<string|int, array<string|int, mixed>>

assertAttributeExists()

Asserts that the model attribute name is valid.

protected static assertAttributeExists(mixed $name) : void
Parameters
$name : mixed

The name to validate.

Tags
throws
OutOfBoundsException

If attribute name is not a part of model $columns.

Return values
void

bootstrap()

Override this method to add your own bootstrap code to the modal.

protected bootstrap() : void
Return values
void

buildNestedQuery()

Builds a nested query.

private static buildNestedQuery(array<string|int, mixed> $condition, int|string $index) : array<string|int, mixed>
Parameters
$condition : array<string|int, mixed>

The condition in the form of ['OPERATOR1', 'COLUMN', 'OPERATOR2', 'VALUE', ], ..., [..., ...].

$index : int|string

The index of the condition.

Return values
array<string|int, mixed>

An associative array containing the SQL query and its needed variables.

buildWhereClause()

private static buildWhereClause(array<string|int, mixed> $conditions) : array<string|int, mixed>
Parameters
$conditions : array<string|int, mixed>
Return values
array<string|int, mixed>

validateCondition()

Validates the passed condition.

private static validateCondition(array<string|int, mixed> $condition, int|string $index) : array<string|int, mixed>
Parameters
$condition : array<string|int, mixed>

The condition to validate. in the form of ['OPERATOR1', 'COLUMN', 'OPERATOR2', 'VALUE']

$index : int|string

The index of the condition (used to make more user-friendly exception).

Tags
throws
InvalidArgumentException

If the condition is invalid.

Return values
array<string|int, mixed>

An array containing the validated condition.

validateOperator()

Validates the passed operator.

private static validateOperator(string $operator, int|string $index) : string
Parameters
$operator : string

The operator to validate.

$index : int|string

The index of the condition (used to make more user-friendly exception).

Tags
throws
InvalidArgumentException

If the operator is invalid.

Return values
string

The validated operator.

Search results