GenericService
extends Service
in package
Uses
EventsContract, CrudContract, JoinContract, UploadsContract
Base Service for generic services
Table of Contents
Properties
- $cachePrefix : string
- The cache prefix to use for caching.
- $cacheTtl : int
- The cache ttl to use for caching.
- $connection : string|null
- $createColumns : array<string|int, mixed>|null
- $defaultJoinType : JoinType
- $fileColumns : array<string|int, mixed>|null
- Define columns that should be received as files in this array.
- $joinAliases : array<string|int, mixed>|null
- $joins : array<string|int, mixed>|null
- $joinTypes : array<string|int, mixed>|null
- $limit : int
- $listColumns : array<string|int, mixed>|null
- $offset : int
- $pk_field : string
- $table : string
- $updateColumns : array<string|int, mixed>|null
- $cacheInstance : PioniaCache|null
- The cache instance to use for caching.
- $dontRelate : bool
- Can be mutated by the frontend to turn off relationships and go back to querying the base table again.
Methods
- __construct() : mixed
- cache() : bool|mixed
- Caches the value if the value is not null.
- ClearByKeys() : bool
- Clear the cache.
- deleteCache() : bool
- Delete the cache for the key.
- getCache() : mixed
- getCacheInstance() : PioniaCache|null
- getItem() : null|object
- Override this in your service to define the basis to return single item details
- getItems() : null|object
- Override this in your service to define the basis to return multiple items from the database
- getJoinQuery() : null|object
- Override this in your service to define the basis to return multiple items from the database
- handleUpload() : mixed
- Provides the default upload behaviour for the service.
- hasCache() : bool
- Check if the cache has the key.
- postCreate() : object|array<string|int, mixed>|null
- This method is post create action, use it to perform any action after saving.
- postDelete() : mixed
- This method is called before the delete action. Use it to perform any action before deleting.
- postUpdate() : object|array<string|int, mixed>|null
- This method is post create action, use it to perform any action after saving.
- preCreate() : array<string|int, mixed>|bool|null
- This method is called before the create action, use it to transform the data before saving.
- preDelete() : array<string|int, mixed>|null|object|bool
- This method is called before the delete action. Use it to perform any action before deleting.
- preUpdate() : array<string|int, mixed>|bool|null
- This method is called before the create action, use it to transform the data before saving.
- setCache() : bool
- setCacheInstance() : void
- Set the cache instance to use for caching.
- updateCache() : void
- _checkPaginationInternal() : bool
- Detects if we have pagination params anywhere in the request.
- allItems() : array<string|int, mixed>|null
- Retrieve all in CRUD
- checkIfFieldPassesAllValidations() : mixed
- createItem() : object|null
- Create in CRUD Saves in transactions, runs pre and post create events
- defaultUpload() : string
- This hook will receive every file found in your request and you can handle it as you wish.
- deleteItem() : mixed
- Delete in CRUD Handles both post and pre delete events Deletes in joined mode if defined too, but will delete only from the main table
- detectPagination() : bool
- Detect if our pagination params are defined anywhere in the request.
- getAllWithPagination() : array<string|int, mixed>|null
- Retrieve all with pagination
- getOne() : object|null
- Retrieve in CRUD, returns on Item at a time.
- hasLimit() : mixed
- hasOffset() : mixed
- isInJoinMode() : bool
- paginate() : array<string|int, mixed>|null
- randomItem() : mixed
- updateItem() : object|array<string|int, mixed>|null
- Updated an item in the db.
- attachJoins() : Join
- Attaches all defined joins to the base query.
- cleanCacheKey() : string
- cleanRelationColumns() : void
- If the fields are already in the format of relationships, this method reverses that including removing duplicates.
- detectAndAddColumns() : void
- Checks if the frontend has defined columns we should query by.
- getAllItemsJoined() : array<string|int, mixed>
- Will be used when we are listing everything.
- getFieldValue() : mixed|UploadedFile|null
- Picks the value of a field from the request data
- getKeyName() : string
- Converts the key to snake case and appends the current class prefix if it does not exist.
- getListColumns() : array<string|int, mixed>|string
- Returns the columns we shall query from the db while querying
- getOneInternal() : null|array<string|int, mixed>|object
- Gets one item from the database. Can be overridden by defining a getOne method in the service
- getOneJoined() : object|null
- Will be used when we are getting a single item.
- joinQuery() : Join
- Resolve the base query to base on to start joining.
- logger() : LoggerInterface|null
- primaryKey() : bool|string
- Returns the primary key field
- switchJoin() : Join
- weShouldJoin() : bool
- Detects whether to enter to enter the join mode or stay in the normal mode.
Properties
$cachePrefix
The cache prefix to use for caching.
public
string
$cachePrefix
= __CLASS__
$cacheTtl
The cache ttl to use for caching.
public
int
$cacheTtl
= 60
Defaults to 60 seconds.
$connection
public
string|null
$connection
= null
The connection to use. Default is db
.
$createColumns
public
array<string|int, mixed>|null
$createColumns
= null
The columns to return in the create
request. Only these columns will be populated from
the request and saved
$defaultJoinType
public
JoinType
$defaultJoinType
= \Pionia\Http\Services\JoinType::INNER
The default join type to use when joining tables.
$fileColumns
Define columns that should be received as files in this array.
public
array<string|int, mixed>|null
$fileColumns
= null
$joinAliases
public
array<string|int, mixed>|null
$joinAliases
= null
An associative array of aliases to be used for joins with the table in the service. The key is the table name and the value is the alias.
Tags
$joins
public
array<string|int, mixed>|null
$joins
= null
An associative array of tables to be used for joins with the table in the service. The key is the table name and the value is an array of the columns to join on.
Tags
$joinTypes
public
array<string|int, mixed>|null
$joinTypes
= null
An associative array of join types to be used for joins with the table in the service. The key is the table name and the value is the join type.
Tags
$limit
public
int
$limit
= 10
The initial number of items to return per list
request can de overridden in the request.
$listColumns
public
array<string|int, mixed>|null
$listColumns
= null
The columns to return in all requests returning data.
$offset
public
int
$offset
= 0
The initial index to start from, can de overridden in the request.
$pk_field
public
string
$pk_field
= 'id'
The primary key field name. Default is id
.
$table
public
string
$table
The base table to be used in the service. This is required when in joins or not
$updateColumns
public
array<string|int, mixed>|null
$updateColumns
= null
The columns to return in the update
request. Only these columns will be populated from
the request and saved. If left null, all columns defined in the request object will be updated.
$cacheInstance
The cache instance to use for caching.
private
PioniaCache|null
$cacheInstance
= null
$dontRelate
Can be mutated by the frontend to turn off relationships and go back to querying the base table again.
private
bool
$dontRelate
= false
Methods
__construct()
public
__construct(PioniaApplication $app, Request $request) : mixed
Parameters
- $app : PioniaApplication
- $request : Request
cache()
Caches the value if the value is not null.
public
cache(string $key[, mixed $value = null ][, mixed $ttl = null ][, bool|null $exact = false ]) : bool|mixed
Parameters
- $key : string
-
The key to cache
- $value : mixed = null
-
The value to cache
- $ttl : mixed = null
-
The time to live for the cache
- $exact : bool|null = false
-
If passed, the key won't be parsed at all, it will be cached as is.
Return values
bool|mixedClearByKeys()
Clear the cache.
public
ClearByKeys(array<string|int, mixed> $keys[, bool|null $exact = false ]) : bool
Parameters
- $keys : array<string|int, mixed>
-
The keys to clear
- $exact : bool|null = false
-
If passed, the keys won't be parsed at all
Return values
booldeleteCache()
Delete the cache for the key.
public
deleteCache(string $key[, bool $exact = false ]) : bool
Parameters
- $key : string
-
The key to delete
- $exact : bool = false
-
If passed, the key won't be parsed at all
Return values
boolgetCache()
public
getCache( $key[, bool|null $exact = false ]) : mixed
Parameters
getCacheInstance()
public
getCacheInstance() : PioniaCache|null
Return values
PioniaCache|nullgetItem()
Override this in your service to define the basis to return single item details
public
getItem() : null|object
Return values
null|objectgetItems()
Override this in your service to define the basis to return multiple items from the database
public
getItems() : null|object
Return values
null|objectgetJoinQuery()
Override this in your service to define the basis to return multiple items from the database
public
getJoinQuery() : null|object
Return values
null|objecthandleUpload()
Provides the default upload behaviour for the service.
public
handleUpload(UploadedFile $file, string $fileName) : mixed
You can override this method in your service to provide custom upload behaviour.
Parameters
- $file : UploadedFile
-
The file to upload
- $fileName : string
-
The name to save the file as
Tags
hasCache()
Check if the cache has the key.
public
hasCache( $key[, bool|null $exact = false ]) : bool
Will not throw an exception if the key is not set or the cache is not activated.
Parameters
Return values
boolpostCreate()
This method is post create action, use it to perform any action after saving.
public
postCreate([array<string|int, mixed>|object|null $createdItem = null ]) : object|array<string|int, mixed>|null
You have access to the data being created in the $this->request->getData()
method
Whatever is returned here is what is returned to the user
Parameters
- $createdItem : array<string|int, mixed>|object|null = null
Return values
object|array<string|int, mixed>|nullpostDelete()
This method is called before the delete action. Use it to perform any action before deleting.
public
postDelete(PDOStatement $deleteInstance[, array<string|int, mixed>|null|object $deletedItem = null ]) : mixed
You have access to the data being updated in the $this->request->getData()
method
This has no effect on the delete action. Since it is called after the delete action.
Whatever is returned here is what is returned to the user
Parameters
- $deleteInstance : PDOStatement
- $deletedItem : array<string|int, mixed>|null|object = null
postUpdate()
This method is post create action, use it to perform any action after saving.
public
postUpdate([array<string|int, mixed>|object|null $updatedItem = null ]) : object|array<string|int, mixed>|null
You have access to the data being created in the $this->request->getData()
method
Whatever is returned here is what is returned to the user
Parameters
- $updatedItem : array<string|int, mixed>|object|null = null
Return values
object|array<string|int, mixed>|nullpreCreate()
This method is called before the create action, use it to transform the data before saving.
public
preCreate([array<string|int, mixed>|null $createItem = null ]) : array<string|int, mixed>|bool|null
You have access to the data being created in the $this->request->getData()
method
Return false, null or throw an exception to stop the create action.
Parameters
- $createItem : array<string|int, mixed>|null = null
-
The data being created - You can modify this data before saving. Return false or null to stop the create action
Return values
array<string|int, mixed>|bool|nullpreDelete()
This method is called before the delete action. Use it to perform any action before deleting.
public
preDelete([array<string|int, mixed>|null|object $itemToDelete = null ]) : array<string|int, mixed>|null|object|bool
You have access to the data being updated in the $this->request->getData()
method
Return false, null or throw an exception to stop the delete action.
Parameters
- $itemToDelete : array<string|int, mixed>|null|object = null
Return values
array<string|int, mixed>|null|object|boolpreUpdate()
This method is called before the create action, use it to transform the data before saving.
public
preUpdate([array<string|int, mixed>|null $updateItem = null ]) : array<string|int, mixed>|bool|null
You have access to the object being updated $updateItem
Return false, null or raise any exception to stop the update action.
Parameters
- $updateItem : array<string|int, mixed>|null = null
-
The data being updated - You can modify this data before saving
Return values
array<string|int, mixed>|bool|null —The return data is what is saved to the database, if false or null, the update action is aborted
setCache()
public
setCache(string $key, mixed $value[, mixed $ttl = null ][, bool|null $exact = false ]) : bool
Parameters
- $key : string
- $value : mixed
- $ttl : mixed = null
- $exact : bool|null = false
Return values
boolsetCacheInstance()
Set the cache instance to use for caching.
public
setCacheInstance(PioniaCache|null $cache) : void
Parameters
- $cache : PioniaCache|null
updateCache()
public
updateCache(mixed $key, mixed $newValue[, mixed $exact = false ][, int|null $ttl = null ]) : void
Parameters
- $key : mixed
- $newValue : mixed
- $exact : mixed = false
- $ttl : int|null = null
_checkPaginationInternal()
Detects if we have pagination params anywhere in the request.
protected
_checkPaginationInternal(array<string|int, mixed> $data) : bool
For pagination to kick-in, both offset and limit must be defined at any of the levels
defined by $this->detectPagination()
Parameters
- $data : array<string|int, mixed>
Tags
Return values
boolallItems()
Retrieve all in CRUD
protected
allItems() : array<string|int, mixed>|null
Can be overridden by defining a getItems method in the service
Tags
Return values
array<string|int, mixed>|nullcheckIfFieldPassesAllValidations()
protected
checkIfFieldPassesAllValidations(mixed $field) : mixed
Parameters
- $field : mixed
Tags
createItem()
Create in CRUD Saves in transactions, runs pre and post create events
protected
createItem() : object|null
Tags
Return values
object|nulldefaultUpload()
This hook will receive every file found in your request and you can handle it as you wish.
protected
defaultUpload(UploadedFile $file, string $fileName) : string
Whatever you return here is what we shall send to the db. Returning null or false will skip the file in the entire request from being saved in the db.
Parameters
- $file : UploadedFile
-
The file object that was uploaded.
- $fileName : string
-
The name as defined in the request.
Tags
Return values
string —The value to be saved in the db, null or false to skip the file, or an exception to stop the entire request
deleteItem()
Delete in CRUD Handles both post and pre delete events Deletes in joined mode if defined too, but will delete only from the main table
protected
deleteItem() : mixed
Tags
detectPagination()
Detect if our pagination params are defined anywhere in the request.
protected
detectPagination(array<string|int, mixed> $reqData) : bool
Remember these can in defined in one of the following:-
On the request root level
Parameters
- $reqData : array<string|int, mixed>
Tags
Return values
boolgetAllWithPagination()
Retrieve all with pagination
protected
getAllWithPagination() : array<string|int, mixed>|null
Tags
Return values
array<string|int, mixed>|nullgetOne()
Retrieve in CRUD, returns on Item at a time.
protected
getOne() : object|null
Tags
Return values
object|nullhasLimit()
protected
hasLimit() : mixed
hasOffset()
protected
hasOffset() : mixed
isInJoinMode()
protected
isInJoinMode() : bool
Return values
boolpaginate()
protected
paginate() : array<string|int, mixed>|null
Tags
Return values
array<string|int, mixed>|nullrandomItem()
protected
randomItem() : mixed
Tags
updateItem()
Updated an item in the db.
protected
updateItem() : object|array<string|int, mixed>|null
If updateColumns are defined, it only updates those.
It also calls both preUpdate and postUpdate hooks if defined
Tags
Return values
object|array<string|int, mixed>|nullattachJoins()
Attaches all defined joins to the base query.
private
attachJoins([Join|null $join = null ]) : Join
Parameters
- $join : Join|null = null
Tags
Return values
JoincleanCacheKey()
private
cleanCacheKey(string $key) : string
Parameters
- $key : string
Return values
stringcleanRelationColumns()
If the fields are already in the format of relationships, this method reverses that including removing duplicates.
private
cleanRelationColumns() : void
Tags
detectAndAddColumns()
Checks if the frontend has defined columns we should query by.
private
detectAndAddColumns() : void
Works for both joined and non-joined querying. It pays respect to aliases defined, so, don't forget to respect them too!
getAllItemsJoined()
Will be used when we are listing everything.
private
getAllItemsJoined() : array<string|int, mixed>
Tags
Return values
array<string|int, mixed>getFieldValue()
Picks the value of a field from the request data
private
getFieldValue( $name) : mixed|UploadedFile|null
Parameters
Return values
mixed|UploadedFile|nullgetKeyName()
Converts the key to snake case and appends the current class prefix if it does not exist.
private
getKeyName(string $key) : string
Parameters
- $key : string
Return values
stringgetListColumns()
Returns the columns we shall query from the db while querying
private
getListColumns() : array<string|int, mixed>|string
Return values
array<string|int, mixed>|stringgetOneInternal()
Gets one item from the database. Can be overridden by defining a getOne method in the service
private
getOneInternal(mixed $id) : null|array<string|int, mixed>|object
Parameters
- $id : mixed
Tags
Return values
null|array<string|int, mixed>|objectgetOneJoined()
Will be used when we are getting a single item.
private
getOneJoined() : object|null
Tags
Return values
object|nulljoinQuery()
Resolve the base query to base on to start joining.
private
joinQuery() : Join
If the developer has not yet defined any joins, we drop it and generate a new one.
Tags
Return values
Joinlogger()
private
logger() : LoggerInterface|null
Return values
LoggerInterface|nullprimaryKey()
Returns the primary key field
private
primaryKey() : bool|string
Tags
Return values
bool|stringswitchJoin()
private
switchJoin(JoinType $joinType, Join $query, string $table) : Join
Parameters
- $joinType : JoinType
-
The type of join to use
- $query : Join
-
The current query we are joining to
- $table : string
-
The table to join
Return values
Join —The query with the join added
weShouldJoin()
Detects whether to enter to enter the join mode or stay in the normal mode.
private
weShouldJoin() : bool
Normal mode queries one table at a time.