RetrieveListRandomService
extends GenericService
in package
Uses
RandomMixin, ListMixin, RetrieveMixin
Adds the retrieve and create actions to the service.
Table of Contents
Properties
- $connection : string
- $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
- $dontRelate : bool
- Can be mutated by the frontend to turn off relationships and go back to querying the base table again.
Methods
- details() : BaseResponse
- 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
- This hook will receive every file found in your request and you can handle it as you wish.
- list() : BaseResponse
- 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.
- random() : BaseResponse
- retrieve() : BaseResponse
- _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() : mixed
- 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
- 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
- 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.
- 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
- 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.
- switchJoin() : Join
- weShouldJoin() : bool
- Detects whether to enter to enter the join mode or stay in the normal mode.
Properties
$connection
public
string
$connection
= 'db'
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\Generics\Base\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.
$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
details()
public
details() : BaseResponse
Tags
Return values
BaseResponsegetItem()
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()
This hook will receive every file found in your request and you can handle it as you wish.
public
abstract handleUpload(UploadedFile $file, string $fileName) : mixed
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.
Return values
mixed —The value to be saved in the db, null or false to skip the file, or an exception to stop the entire request
list()
public
list() : BaseResponse
Tags
Return values
BaseResponsepostCreate()
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
random()
public
random() : BaseResponse
Tags
Return values
BaseResponseretrieve()
public
retrieve() : BaseResponse
Tags
Return values
BaseResponse_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) : mixed
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
mixed —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
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
paginate()
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
JoincleanRelationColumns()
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|nullgetListColumns()
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
JoinswitchJoin()
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.