MySQLModel
MySQLModel provides a set of methods to interact with a database table.
How to use:
- Extend this class to create a query-able "model/repository" for that table.
- The extended class name should be the same as the table name.
Mandatory abstract methods to implement:
- primaryField(): string 'FieldID'
Optional methods to implement for additional processing:
- formatFields(): array [ 'Field' => 'string', 'AnotherField' => 'int', ... ]
- redactFields(): array [ 'Field', 'AnotherField' ]
The value of all redacted fields will be replaced with the string "__REDACTED__".
Table of Contents
Properties
- $formatFields : mixed
- $primaryField : mixed
- $redactedFields : mixed
- $table : mixed
Methods
- all() : null|array<string|int, mixed>
- Get all records.
- avg() : null|array<string|int, mixed>
- Average records.
- clone() : array<string|int, mixed>|object
- Clone a record.
- count() : null|array<string|int, mixed>
- Count records.
- create() : int|string
- Create a record.
- delete() : void
- Delete a record by its primary id.
- find() : null|array<string|int, mixed>|object
- Find a record by its primary id.
- formatFields() : array<string|int, mixed>
- get() : null|array<string|int, mixed>|object
- Get a record by filtering on field with value.
- max() : null|array<string|int, mixed>
- Maximum of records.
- min() : null|array<string|int, mixed>
- Minimum of records.
- paginate() : null|array<string|int, mixed>
- Paginate records.
- primaryField() : string
- redactFields() : array<string|int, mixed>
- search() : null|array<string|int, mixed>
- Search for records filtering on field like %value%.
- sum() : null|array<string|int, mixed>
- Summarize records.
- update() : void
- Update a record by its primary id.
- formatRecord() : array<string|int, mixed>
- invoke() : mixed
- processRecords() : array<string|int, mixed>
- redactRecord() : array<string|int, mixed>
Properties
$formatFields
private
mixed
$formatFields
= []
$primaryField
private
mixed
$primaryField
= null
$redactedFields
private
mixed
$redactedFields
= []
$table
private
mixed
$table
= null
Methods
all()
Get all records.
public
all([array<string|int, mixed> $filters = [] ]) : null|array<string|int, mixed>
- Example use: $user->all()
- Example use: $user->all($filters = ['Active' => 0])
Parameters
- $filters : array<string|int, mixed> = []
Return values
null|array<string|int, mixed>avg()
Average records.
public
avg(string $averageField[, array<string|int, mixed> $filters = [] ][, array<string|int, mixed> $groupByFields = [] ]) : null|array<string|int, mixed>
- Example use: Invoice::avg('Amount')
- Example use: Invoice::avg('Amount', ['Paid' => 0, 'Due' => '2024-07-25'])
- Example use: Invoice::avg('Amount', groupFields: ['Paid'])
Parameters
- $averageField : string
- $filters : array<string|int, mixed> = []
- $groupByFields : array<string|int, mixed> = []
Return values
null|array<string|int, mixed>clone()
Clone a record.
public
clone(string|int $primaryID[, array<string|int, mixed> $newRecord = [] ]) : array<string|int, mixed>|object
Parameters
- $primaryID : string|int
- $newRecord : array<string|int, mixed> = []
-
used to write new values to fields from the cloned record.
- Example use: $user->clone($userID = 3)
- Example use: $user->clone($userID = 3, ['Name' => 'New Name'])
Return values
array<string|int, mixed>|objectcount()
Count records.
public
count([string $countField = '*' ][, array<string|int, mixed> $filters = [] ][, array<string|int, mixed> $groupByFields = [] ]) : null|array<string|int, mixed>
- Example use: $user->count()
- Example use: $user->count(filters: ['Active' => 0])
- Example use: $user->count('UserID', groupFields: ['Active'])
Parameters
- $countField : string = '*'
- $filters : array<string|int, mixed> = []
- $groupByFields : array<string|int, mixed> = []
Return values
null|array<string|int, mixed>create()
Create a record.
public
create(array<string|int, mixed> $record) : int|string
- Example use: $user->create(['Name' => 'John'])
Parameters
- $record : array<string|int, mixed>
Return values
int|stringdelete()
Delete a record by its primary id.
public
delete(string|int $primaryID) : void
- Example use: $user->delete($userID = 3)
Parameters
- $primaryID : string|int
find()
Find a record by its primary id.
public
find(string|int $primaryID) : null|array<string|int, mixed>|object
- Example use: $user->find($userID = 3)
Parameters
- $primaryID : string|int
Return values
null|array<string|int, mixed>|objectformatFields()
public
formatFields() : array<string|int, mixed>
Return values
array<string|int, mixed>get()
Get a record by filtering on field with value.
public
get(string $field, mixed $value) : null|array<string|int, mixed>|object
- Example use: $user->get('Email', 'John.doe@example.com')
Parameters
- $field : string
- $value : mixed
Return values
null|array<string|int, mixed>|objectmax()
Maximum of records.
public
max(string $maxField[, array<string|int, mixed> $filters = [] ][, array<string|int, mixed> $groupByFields = [] ]) : null|array<string|int, mixed>
- Example use: Invoice::max('Amount')
- Example use: Invoice::max('Amount', ['Paid' => 0, 'Due' => '2024-07-25'])
- Example use: Invoice::max('Amount', groupFields: ['Paid'])
Parameters
- $maxField : string
- $filters : array<string|int, mixed> = []
- $groupByFields : array<string|int, mixed> = []
Return values
null|array<string|int, mixed>min()
Minimum of records.
public
min(string $minField[, array<string|int, mixed> $filters = [] ][, array<string|int, mixed> $groupByFields = [] ]) : null|array<string|int, mixed>
- Example use: Invoice::min('Amount')
- Example use: Invoice::min('Amount', ['Paid' => 0, 'Due' => '2024-07-25'])
- Example use: Invoice::min('Amount', groupFields: ['Paid'])
Parameters
- $minField : string
- $filters : array<string|int, mixed> = []
- $groupByFields : array<string|int, mixed> = []
Return values
null|array<string|int, mixed>paginate()
Paginate records.
public
paginate(int $page, int $pageSize[, array<string|int, mixed> $filters = [] ]) : null|array<string|int, mixed>
- Example use: $user->paginate(1, 10)
- Example use: $user->paginate(1, 10, ['Name' => 'John'])
Parameters
- $page : int
- $pageSize : int
- $filters : array<string|int, mixed> = []
Return values
null|array<string|int, mixed>primaryField()
public
abstract primaryField() : string
Return values
stringredactFields()
public
redactFields() : array<string|int, mixed>
Return values
array<string|int, mixed>search()
Search for records filtering on field like %value%.
public
search(string $field, string $value) : null|array<string|int, mixed>
- Example use: $user->search('Name', 'John')
Parameters
- $field : string
- $value : string
Return values
null|array<string|int, mixed>sum()
Summarize records.
public
sum(string $sumField[, array<string|int, mixed> $filters = [] ][, array<string|int, mixed> $groupByFields = [] ]) : null|array<string|int, mixed>
- Example use: Invoice::sum('Amount')
- Example use: Invoice::sum('Amount', ['Paid' => 0, 'Due' => '2024-07-25'])
- Example use: Invoice::sum('Amount', groupFields: ['Paid'])
Parameters
- $sumField : string
- $filters : array<string|int, mixed> = []
- $groupByFields : array<string|int, mixed> = []
Return values
null|array<string|int, mixed>update()
Update a record by its primary id.
public
update(string|int $primaryID, array<string|int, mixed> $data) : void
- Example use: $user->update($userID = 3, ['Name' => 'A new name'])
Parameters
- $primaryID : string|int
- $data : array<string|int, mixed>
formatRecord()
private
formatRecord(array<string|int, mixed> $record) : array<string|int, mixed>
Parameters
- $record : array<string|int, mixed>
Return values
array<string|int, mixed>invoke()
private
invoke() : mixed
processRecords()
private
processRecords(array<string|int, mixed> $records) : array<string|int, mixed>
Parameters
- $records : array<string|int, mixed>
Return values
array<string|int, mixed>redactRecord()
private
redactRecord(array<string|int, mixed> $record) : array<string|int, mixed>
Parameters
- $record : array<string|int, mixed>