Model class abstract

Framework\MVC\Model

Class Model.

Implements: Framework\MVC\ModelInterface

Defined in libraries/mvc/src/Model.php

Properties #

$models
-
$connectionRead
Database connection instance name for read operations.
$connectionWrite
Database connection instance name for write operations.
$table
Table name.
$primaryKey
Table Primary Key.
$protectPrimaryKey
Prevents Primary Key changes on INSERT and UPDATE.
$returnType
Fetched item return type.
$allowedFields
Allowed columns for INSERT and UPDATE.
$autoTimestamps
Auto set timestamp fields.
$fieldCreated
The timestamp field for 'created at' time when $autoTimestamps is true.
$fieldUpdated
The timestamp field for 'updated at' time when $autoTimestamps is true.
$timestampFormat
The timestamp format used on database write operations.
$validation
The Model Validation instance.
$validationLabels
Validation field labels.
$validationMessages
Validation error messages.
$validationRules
Validation rules.
$validationValidators
Validation Validators.
$pager
The Pager instance.
$pagerView
Default pager view.
$pagerQuery
-
$pagerAllowedQueries
-
$pagerUrl
Pager URL.
$cacheActive
-
$cacheInstance
-
$cacheTtl
-
$cacheDataNotFound
-
$languageInstance
-
$columnCase
-

$models

protected static array $models = array ( )

$connectionRead

protected string $connectionRead = 'default'

Database connection instance name for read operations.

$connectionWrite

protected string $connectionWrite = 'default'

Database connection instance name for write operations.

$table

protected string $table

Table name.

$primaryKey

protected string $primaryKey = 'id'

Table Primary Key.

$protectPrimaryKey

protected bool $protectPrimaryKey = true

Prevents Primary Key changes on INSERT and UPDATE.

$returnType

protected string $returnType = 'stdClass'

Fetched item return type.

$allowedFields

protected array $allowedFields

Allowed columns for INSERT and UPDATE.

$autoTimestamps

protected bool $autoTimestamps = false

Auto set timestamp fields.

$fieldCreated

protected string $fieldCreated = 'createdAt'

The timestamp field for 'created at' time when $autoTimestamps is true.

$fieldUpdated

protected string $fieldUpdated = 'updatedAt'

The timestamp field for 'updated at' time when $autoTimestamps is true.

$timestampFormat

protected string $timestampFormat = 'Y-m-d H:i:s'

The timestamp format used on database write operations.

$validation

protected \Framework\Validation\Validation $validation

The Model Validation instance.

$validationLabels

protected array $validationLabels

Validation field labels.

$validationMessages

protected array $validationMessages

Validation error messages.

$validationRules

protected array $validationRules

Validation rules.

$validationValidators

protected array $validationValidators = array ( 0 => 'Framework\\MVC\\Validator', 1 => 'Framework\\Validation\\FilesValidator', )

Validation Validators.

$pager

protected \Framework\Pagination\Pager $pager

The Pager instance.

$pagerView

protected string $pagerView

Default pager view.

$pagerQuery

protected string $pagerQuery

$pagerAllowedQueries

protected ?array $pagerAllowedQueries = NULL

$pagerUrl

protected string $pagerUrl

Pager URL.

$cacheActive

protected bool $cacheActive = false

$cacheInstance

protected string $cacheInstance = 'default'

$cacheTtl

protected int $cacheTtl = 60

$cacheDataNotFound

protected string|int $cacheDataNotFound = 0

$languageInstance

protected string $languageInstance = 'default'

$columnCase

protected string $columnCase = 'camel'

Methods #

__call()
-
count()
A basic function to count rows in the table.
create()
Insert a new row.
createBy()
Insert a new row and return the inserted column value.
delete()
Delete based on Primary Key.
deleteBy()
Delete based on column value.
find()
Alias of {@see Model::read()}.
findAll()
Alias of {@see Model::list()}.
findBy()
Alias of {@see Model::readBy()}.
get()
Get same Model instance.
getErrors()
Get Validation errors.
getPager()
Get the Pager.
getValidationMessages()
-
getValidationValidators()
-
list()
List rows, optionally with limit and offset.
paginate()
A basic function to paginate all rows of the table.
read()
Read a row based on Primary Key.
readBy()
Read a row by column name and value.
replace()
Replace based on Primary Key and return the number of affected rows.
replaceBy()
Replace based on column value and return the number of affected rows.
save()
Save a row. Update if the Primary Key is present, otherwise insert a new row.
update()
Update based on Primary Key and return the number of affected rows.
updateBy()
Update based on column value and return the number of affected rows.
checkMysqliException()
-
checkPrimaryKey()
-
convertCase()
Convert a value to specific case.
filterAllowedFields()
-
getAllowedFields()
-
getCache()
-
getCacheDataNotFound()
-
getCacheInstance()
-
getCacheKey()
-
getCacheTtl()
-
getConnectionRead()
-
getConnectionWrite()
-
getDatabaseToRead()
-
getDatabaseToWrite()
-
getFieldCreated()
-
getFieldUpdated()
-
getLanguage()
-
getLanguageInstance()
-
getPagerAllowedQueries()
Get allowed queries to be used in the Pager.
getPagerQuery()
Get the custom query to be used in the Pager.
getPagerUrl()
Get the custom URL to be used in the Pager.
getPagerView()
Get the custom view to be used in the Pager.
getPrimaryKey()
-
getReturnType()
-
getTable()
-
getTimestamp()
Used to auto set the timestamp fields.
getTimestampFormat()
-
getValidation()
-
getValidationLabels()
-
getValidationRules()
-
isAutoTimestamps()
-
isCacheActive()
-
isProtectPrimaryKey()
-
makeArray()
-
makeEntity()
-
makePageLimitAndOffset()
-
makeTableName()
-
readRow()
-
readWithCache()
-
sanitizePageNumber()
-
setDuplicateEntryError()
Set "Duplicate entry" as 'unique' error in the Validation.
setPager()
Set the Pager.
timezone()
Get the timezone from database write connection config. As fallback, uses the UTC timezone.
updateCachedRow()
-

__call()

public function __call(string $method, array $arguments): mixed
Parameters
  • string $method
  • array $arguments
Returns mixed

count()

public function count(array $where = array ( )): int

A basic function to count rows in the table.

Parameters
  • array $where - Array in this format: `[['id', '=', 25]]`
Returns int

create()

public function create(\Framework\MVC\Entity|stdClass|array $data): string|int|false

Insert a new row.

Parameters
  • \Framework\MVC\Entity|stdClass|array $data
Returns string|int|false - The LAST_INSERT_ID() on success or false if validation fail

createBy()

public function createBy(string $column, \Framework\MVC\Entity|stdClass|array $data): string|int|false

Insert a new row and return the inserted column value.

Parameters
  • string $column - Column name
  • \Framework\MVC\Entity|stdClass|array $data
Returns string|int|false - The value from the column data or false if validation fail

delete()

public function delete(string|int $id): string|int|false

Delete based on Primary Key.

Parameters
  • string|int $id
Returns string|int|false - The number of affected rows

deleteBy()

public function deleteBy(string $column, string|int $value): string|int|false

Delete based on column value.

Parameters
  • string $column
  • string|int $value
Returns string|int|false - The number of affected rows

find()

public function find(string|int $id): \Framework\MVC\Entity|stdClass|array|null

Alias of {@see Model::read()}.

Parameters
  • string|int $id
Returns \Framework\MVC\Entity|stdClass|array|null

findAll()

public function findAll(?int $limit = NULL, ?int $offset = NULL): array

Alias of {@see Model::list()}.

Find all rows with limit and offset.

Parameters
  • ?int $limit
  • ?int $offset
Returns array

findBy()

public function findBy(string $column, string|int $value): \Framework\MVC\Entity|stdClass|array|null

Alias of {@see Model::readBy()}.

Find a row by column name and value.

Parameters
  • string $column
  • string|int $value
Returns \Framework\MVC\Entity|stdClass|array|null

get()

public static function get(string $class): \Framework\MVC\Model

Get same Model instance.

Parameters
  • string $class
Returns \Framework\MVC\Model

getErrors()

public function getErrors(): array

Get Validation errors.

Returns array

getPager()

public function getPager(): \Framework\Pagination\Pager

Get the Pager.

Allowed only after calling a method that sets the Pager.

Returns \Framework\Pagination\Pager

getValidationMessages()

public function getValidationMessages(): array
Returns array

getValidationValidators()

public function getValidationValidators(): array
Returns array

list()

public function list(?int $limit = NULL, ?int $offset = NULL): array

List rows, optionally with limit and offset.

Parameters
  • ?int $limit
  • ?int $offset
Returns array

paginate()

public function paginate(mixed $page, mixed $perPage = 10, array $where = array ( ), array|string|null $orderBy = NULL, string $orderByDirection = 'asc'): array

A basic function to paginate all rows of the table.

Parameters
  • mixed $page - The current page
  • mixed $perPage - Items per page
  • array $where - Array in this format: `[['id', '=', 25]]`
  • array|string|null $orderBy - Order by columns
  • string $orderByDirection - asc or desc
Returns array

read()

public function read(string|int $id): \Framework\MVC\Entity|stdClass|array|null

Read a row based on Primary Key.

Parameters
  • string|int $id
Returns \Framework\MVC\Entity|stdClass|array|null - The selected row as configured on $returnType property or null if row was not found

readBy()

public function readBy(string $column, string|int $value): \Framework\MVC\Entity|stdClass|array|null

Read a row by column name and value.

Parameters
  • string $column
  • string|int $value
Returns \Framework\MVC\Entity|stdClass|array|null

replace()

public function replace(string|int $id, \Framework\MVC\Entity|stdClass|array $data): string|int|false

Replace based on Primary Key and return the number of affected rows.

Most used with HTTP PUT method.

Parameters
  • string|int $id
  • \Framework\MVC\Entity|stdClass|array $data
Returns string|int|false - The number of affected rows or false if validation fails

replaceBy()

public function replaceBy(string $column, string|int $value, \Framework\MVC\Entity|stdClass|array $data): string|int|false

Replace based on column value and return the number of affected rows.

Parameters
  • string $column
  • string|int $value
  • \Framework\MVC\Entity|stdClass|array $data
Returns string|int|false - The number of affected rows or false if validation fails

save()

public function save(\Framework\MVC\Entity|stdClass|array $data): string|int|false

Save a row. Update if the Primary Key is present, otherwise insert a new row.

Parameters
  • \Framework\MVC\Entity|stdClass|array $data
Returns string|int|false - The number of affected rows on updates as int, the LAST_INSERT_ID() as int on inserts or false if validation fails

update()

public function update(string|int $id, \Framework\MVC\Entity|stdClass|array $data): string|int|false

Update based on Primary Key and return the number of affected rows.

Parameters
  • string|int $id
  • \Framework\MVC\Entity|stdClass|array $data
Returns string|int|false - The number of affected rows or false if validation fails

updateBy()

public function updateBy(string $column, string|int $value, \Framework\MVC\Entity|stdClass|array $data): string|int|false

Update based on column value and return the number of affected rows.

Parameters
  • string $column
  • string|int $value
  • \Framework\MVC\Entity|stdClass|array $data
Returns string|int|false - The number of affected rows or false if validation fails

checkMysqliException()

protected function checkMysqliException(mysqli_sql_exception $exception): void
Parameters
  • mysqli_sql_exception $exception
Returns void
throws
mysqli_sql_exception if message is not for duplicate entry

checkPrimaryKey()

protected function checkPrimaryKey(string|int $id): void
Parameters
  • string|int $id
Returns void

convertCase()

protected function convertCase(string $value, string $case): string

Convert a value to specific case.

Parameters
  • string $value
  • string $case - camel, pascal or snake
Returns string - The converted value

filterAllowedFields()

protected function filterAllowedFields(array $data): array
Parameters
  • array $data
Returns array

getAllowedFields()

protected function getAllowedFields(): array
Returns array

getCache()

protected function getCache(): \Framework\Cache\Cache
Returns \Framework\Cache\Cache

getCacheDataNotFound()

protected function getCacheDataNotFound(): string|int
Returns string|int

getCacheInstance()

protected function getCacheInstance(): string
Returns string

getCacheKey()

protected function getCacheKey(array $fields): string
Parameters
  • array $fields
Returns string

getCacheTtl()

protected function getCacheTtl(): int
Returns int

getConnectionRead()

protected function getConnectionRead(): string
Returns string

getConnectionWrite()

protected function getConnectionWrite(): string
Returns string

getDatabaseToRead()

protected function getDatabaseToRead(): \Framework\Database\Database
Returns \Framework\Database\Database

getDatabaseToWrite()

protected function getDatabaseToWrite(): \Framework\Database\Database
Returns \Framework\Database\Database

getFieldCreated()

protected function getFieldCreated(): string
Returns string

getFieldUpdated()

protected function getFieldUpdated(): string
Returns string

getLanguage()

protected function getLanguage(): \Framework\Language\Language
Returns \Framework\Language\Language

getLanguageInstance()

protected function getLanguageInstance(): string
Returns string

getPagerAllowedQueries()

protected function getPagerAllowedQueries(): ?array

Get allowed queries to be used in the Pager.

Returns ?array

getPagerQuery()

protected function getPagerQuery(): ?string

Get the custom query to be used in the Pager.

Returns ?string

getPagerUrl()

protected function getPagerUrl(): ?string

Get the custom URL to be used in the Pager.

Returns ?string

getPagerView()

protected function getPagerView(): ?string

Get the custom view to be used in the Pager.

Returns ?string

getPrimaryKey()

protected function getPrimaryKey(): string
Returns string

getReturnType()

protected function getReturnType(): string
Returns string

getTable()

protected function getTable(): string
Returns string

getTimestamp()

protected function getTimestamp(): string

Used to auto set the timestamp fields.

Returns string - The timestamp in the $timestampFormat property format
throws
Exception if a DateTime error occur

getTimestampFormat()

protected function getTimestampFormat(): string
Returns string

getValidation()

protected function getValidation(): \Framework\Validation\Validation
Returns \Framework\Validation\Validation

getValidationLabels()

protected function getValidationLabels(): array
Returns array

getValidationRules()

protected function getValidationRules(): array
Returns array

isAutoTimestamps()

protected function isAutoTimestamps(): bool
Returns bool

isCacheActive()

protected function isCacheActive(): bool
Returns bool

isProtectPrimaryKey()

protected function isProtectPrimaryKey(): bool
Returns bool

makeArray()

protected function makeArray(\Framework\MVC\Entity|stdClass|array $data): array
Parameters
  • \Framework\MVC\Entity|stdClass|array $data
Returns array

makeEntity()

protected function makeEntity(array $data): \Framework\MVC\Entity|stdClass|array
Parameters
  • array $data
Returns \Framework\MVC\Entity|stdClass|array

makePageLimitAndOffset()

protected function makePageLimitAndOffset(int $page, int $perPage = 10): array
Parameters
  • int $page
  • int $perPage
Returns array

makeTableName()

protected function makeTableName(): string
Returns string

readRow()

protected function readRow(string $column, string|int $value): ?array
Parameters
  • string $column
  • string|int $value
Returns ?array

readWithCache()

protected function readWithCache(string $column, string|int $value): \Framework\MVC\Entity|stdClass|array|null
Parameters
  • string $column
  • string|int $value
Returns \Framework\MVC\Entity|stdClass|array|null

sanitizePageNumber()

protected function sanitizePageNumber(int $number): int
Parameters
  • int $number
Returns int

setDuplicateEntryError()

protected function setDuplicateEntryError(string $message): void

Set "Duplicate entry" as 'unique' error in the Validation.

NOTE: We will get the index key name and not the column name. Usually the
names are the same. If table have different column and index names,
override this method and get the column name from the information_schema
table.

Parameters
  • string $message - The "Duplicate entry" message from the mysqli_sql_exception
Returns void

setPager()

protected function setPager(\Framework\Pagination\Pager $pager): static

Set the Pager.

Parameters
  • \Framework\Pagination\Pager $pager
Returns static

timezone()

protected function timezone(): DateTimeZone

Get the timezone from database write connection config. As fallback, uses the UTC timezone.

Returns DateTimeZone
throws
Exception if database config has a bad timezone

updateCachedRow()

protected function updateCachedRow(string $column, string|int $value): void
Parameters
  • string $column
  • string|int $value
Returns void