Menu

## GenericService extends BaseRestService
in package
Uses EventsContract, CrudContract, JoinContract, UploadsContract

AbstractYes
GenericService.php : 9

### 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

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.
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.
_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

GenericService.php : 40
`public string $connection = 'db'`

The connection to use. Default is `db`.

#### $createColumns

GenericService.php : 51
`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

JoinContract.php : 72
`public JoinType $defaultJoinType = \Pionia\Generics\Base\JoinType::INNER`

The default join type to use when joining tables.

#### $fileColumns

GenericService.php : 63

Define columns that should be received as files in this array.

`public array<string|int, mixed>|null $fileColumns = null`

#### $joinAliases

JoinContract.php : 51
`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

example

      [
         'student' => 's',
      ]

  This will join the `student` table using the alias `s`.

#### $joins

JoinContract.php : 23
`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

example

      [
         'student' => ['id' => 'student_id'],
      ]

  This will join the `student` table on the `id` column of the current table and the `student_id` column of the `student` table.

#### $joinTypes

JoinContract.php : 37
`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

example

      [
         'student' => JoinType::INNER,
      ]

  This will join the `student` table using an inner join.

#### $limit

GenericService.php : 25
`public int $limit = 10`

The initial number of items to return per `list` request can de overridden in the request.

#### $listColumns

GenericService.php : 45
`public array<string|int, mixed>|null $listColumns = null`

The columns to return in all requests returning data.

#### $offset

GenericService.php : 30
`public int $offset = 0`

The initial index to start from, can de overridden in the request.

#### $pk_field

GenericService.php : 35
`public string $pk_field = 'id'`

The primary key field name. Default is `id`.

#### $table

GenericService.php : 14
`public string $table`

The base table to be used in the service. This is required when in joins or not

#### $updateColumns

GenericService.php : 57
`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

GenericService.php : 20

Can be mutated by the frontend to turn off relationships and go back to querying the base table again.

`private bool $dontRelate = false`

### Methods

#### getItem()

EventsContract.php : 100

Override this in your service to define the basis to return single item details

`public getItem() : null|object`

##### Return values

null|object

#### getItems()

EventsContract.php : 109

Override this in your service to define the basis to return multiple items from the database

`public getItems() : null|object`

##### Return values

null|object

#### getJoinQuery()

EventsContract.php : 118

Override this in your service to define the basis to return multiple items from the database

`public getJoinQuery() : null|object`

##### Return values

null|object

#### handleUpload()

GenericService.php : 89

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

throws
  Exception

#### postCreate()

EventsContract.php : 63

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>|null

#### postDelete()

EventsContract.php : 90

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()

EventsContract.php : 75

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>|null

#### preCreate()

EventsContract.php : 39

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|null

#### preDelete()

EventsContract.php : 51

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|bool

#### preUpdate()

EventsContract.php : 26

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

#### _checkPaginationInternal()

CrudContract.php : 93

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

see

  $this->detectPagination()

##### Return values

bool

#### allItems()

CrudContract.php : 219

Retrieve all in CRUD

`protected allItems() : array<string|int, mixed>|null`

Can be overridden by defining a getItems method in the service

##### Tags

throws
  BaseDatabaseException
throws
  Exception

##### Return values

array<string|int, mixed>|null

#### checkIfFieldPassesAllValidations()

CrudContract.php : 273
`protected checkIfFieldPassesAllValidations(mixed $field) : mixed`

##### Parameters

$field : mixed

##### Tags

throws
  Exception

#### createItem()

CrudContract.php : 299

Create in CRUD Saves in transactions, runs pre and post create events

`protected createItem() : object|null`

##### Tags

throws
  Exception

##### Return values

object|null

#### defaultUpload()

UploadsContract.php : 34

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

throws
  Exception

##### 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()

CrudContract.php : 248

Delete in CRUD Handles both post and pre delete events

`protected deleteItem() : mixed`

##### Tags

throws
  Exception

#### detectPagination()

CrudContract.php : 146

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

example

      {
          SERVICE: "ourService",
          ACTION: "ourAction",
          LIMIT: 10, // can also be `limit: 10`
          OFFSET: 5 // can also be `offset: 5`
      }

      Or they can be defined under the PAGINATION/pagination key

example

      {
          SERVICE: "ourService",
          ACTION: "ourAction",
          PAGINATION:{ // can also be `pagination`
               LIMIT: 10, // can also be `limit: 10`
               OFFSET: 5 // can also be `offset: 5`
           }
      }

      Or in the SEARCH/search param

example

  { SERVICE: "ourService", ACTION: "ourAction", SEARCH:{ // can also be `search` LIMIT: 10, // can also be `limit: 10` OFFSET: 5 // can also be `offset: 5` } } }

##### Return values

bool

#### getAllWithPagination()

CrudContract.php : 363

Retrieve all with pagination

`protected getAllWithPagination() : array<string|int, mixed>|null`

##### Tags

throws
  Exception

##### Return values

array<string|int, mixed>|null

#### getOne()

CrudContract.php : 175

Retrieve in CRUD, returns on Item at a time.

`protected getOne() : object|null`

##### Tags

throws
  Exception

##### Return values

object|null

#### hasLimit()

CrudContract.php : 202
`protected hasLimit() : mixed`

#### hasOffset()

CrudContract.php : 207
`protected hasOffset() : mixed`

#### paginate()

CrudContract.php : 344
`protected paginate() : array<string|int, mixed>|null`

##### Tags

throws
  BaseDatabaseException
throws
  Exception

##### Return values

array<string|int, mixed>|null

#### randomItem()

CrudContract.php : 376
`protected randomItem() : mixed`

##### Tags

throws
  Exception

#### updateItem()

CrudContract.php : 394

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

throws
  Exception

##### Return values

object|array<string|int, mixed>|null

#### attachJoins()

JoinContract.php : 157

Attaches all defined joins to the base query.

`private attachJoins([Join|null $join = null ]) : Join`

##### Parameters

$join : Join|null = null

##### Tags

throws
  Exception

##### Return values

Join

#### cleanRelationColumns()

CrudContract.php : 48

If the fields are already in the format of relationships, this method reverses that including removing duplicates.

`private cleanRelationColumns() : void`

##### Tags

example

      $this->listColumns = ["alias2.name", "alias1.name(category_name), "alias1.id"];
      // this will become
      $this->listColumns = ["name", "name(category_name), "id"];

  ay the time of querying `name(category_name)` will take precedence of `name` thus we shall end up with `category_name` and `id` in the response

#### detectAndAddColumns()

CrudContract.php : 20

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()

JoinContract.php : 141

Will be used when we are listing everything.

`private getAllItemsJoined() : array<string|int, mixed>`

##### Tags

throws
  Exception

##### Return values

array<string|int, mixed>

#### getFieldValue()

GenericService.php : 72

Picks the value of a field from the request data

`private getFieldValue( $name) : mixed|UploadedFile|null`

##### Parameters

$name :

##### Return values

mixed|UploadedFile|null

#### getListColumns()

CrudContract.php : 80

Returns the columns we shall query from the db while querying

`private getListColumns() : array<string|int, mixed>|string`

##### Return values

array<string|int, mixed>|string

#### getOneInternal()

CrudContract.php : 193

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

throws
  Exception

##### Return values

null|array<string|int, mixed>|object

#### getOneJoined()

JoinContract.php : 125

Will be used when we are getting a single item.

`private getOneJoined() : object|null`

##### Tags

throws
  Exception

##### Return values

object|null

#### joinQuery()

JoinContract.php : 205

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

throws
  Exception

##### Return values

Join

#### switchJoin()

JoinContract.php : 180
`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()

JoinContract.php : 78

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.

##### Return values

bool

## Search results