Download the PHP package rancoud/model without Composer
On this page you can find all versions of the php package rancoud/model. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download rancoud/model
More information about rancoud/model
Files in rancoud/model
Informations about the package model
Model Package
Abstract Model for better data manipulation between code and database.
Dependencies
Database package: https://github.com/rancoud/Database
Installation
How to use it?
Extends Rancoud\Model\Model to your class.
You have to implement two abstract methods setFields and setTable.
- setFields is for setting the fields in table
- setTable is for setting the table in database
What is Field?
Field represent a field in the table.
It have 3 arguments:
- field type
- rules
- default value
Field type
It support those field type
- int
- float
- char
- varchar
- text
- date
- datetime
- time
- timestamp
- year
- enum:x,y,z (x,y,z represent each possible value)
Rules
- pk : primary key
- fk : foreign key
- unsigned : only positive value
- email : check if it is valid email
- not_null : can't be null
- max:x : max size (x is size)
- min:x : min size (x is size)
- range:x,y : min size + max size (x is min size, y is max size)
Custom rule
Default
When value is not setted it can be set with those argument
Helpers
It have methods for pagination, create, read, update and delete.
Model::all() accept an array with some keys that triggers specific actions
You can change values output in Model::all() with override functions:
- getSqlAllSelectAndFillSqlParams(params: array)
- getSqlAllJoinAndFillSqlParams(params: array)
- getSqlAllWhereAndFillSqlParams(params: array)
Callbacks
You can add callback before and after create, update and delete.
You can use JsonOutput trait for adding json format for the model.
Field Constructor
Settings
Mandatory
Parameter | Type | Description |
---|---|---|
type | string | type of field, values used : int | float | char | varchar | text | date | datetime | time | timestamp | year |
Optionnals
Parameter | Type | Default value | Description |
---|---|---|---|
rules | array | [] | rules for checking values, values used : pk | fk | unsigned | email | not_null | max | min | range | Rancoud\Model\CustomRule |
default | mixed | false | default value when none given |
Field Methods
- isPrimaryKey(): bool
- isForeignKey(): bool
- isNotNull(): bool
- getDefault(): mixed
- formatValue(value: mixed): ?mixed
Model Constructor
Settings
Mandatory
Parameter | Type | Description |
---|---|---|
$database | \Rancoud\Database\Database | Database Instance |
Model Methods
General Commands
- all(params: array, [validFields: array = []]): array|bool|int
- one(id: mixed, [...ids: mixed = []]): array
- create(args: array): bool|int
- update(args: array, id: mixed, [...ids: mixed = []]): void
- delete(id: mixed, [...ids: mixed = []]): void
- getLastInsertId(): ?int
Database error
- getDatabaseErrors(): ?array
- getDatabaseLastError(): ?array
Callbacks
Add
- addBeforeCreate(name: string, callback: mixed): void
- addAfterCreate(name: string, callback: mixed): void
- addBeforeUpdate(name: string, callback: mixed): void
- addAfterUpdate(name: string, callback: mixed): void
- addBeforeDelete(name: string, callback: mixed): void
- addAfterDelete(name: string, callback: mixed): void
Remove
- removeBeforeCreate(name: string): void
- removeAfterCreate(name: string): void
- removeBeforeUpdate(name: string): void
- removeAfterUpdate(name: string): void
- removeBeforeDelete(name: string): void
- removeAfterDelete(name: string): void
Static Helper Methods
- getCountPerPage(args: array): int
- getLimitOffsetCount(args: array): array
- getOrderByOrderField(args: array, [validFields: array = []]): array
- getPageNumberForHuman(args: array): int
- getPageNumberForSql(args: array): int
- hasInvalidPrimaryKey(value: int): bool
- hasLimit(args: array): bool
- implodeOrder(orders: array): string
- isRowsCount(args: array): bool
- isValidFieldForOrderBy(field: string, [validFields: array = []]): bool
How to Dev
docker compose build && docker compose run lib composer ci
for launching tests
All versions of model with dependencies
rancoud/database Version ^6.0
ext-json Version *
ext-mbstring Version *
ext-pdo Version *