Luminova Framework

PHP Luminova: Base Class for Model Implementation

Last updated: 2024-12-07 04:14:27

Base Model, allows you to define database models for your application, it also supports database searching using a third-party library provided by the Luminova team.

The BaseModel serves as the foundation for all models within your application, providing essential functionalities and features to interact with the database in an efficient and secure method.

It allows you to define basic rules for your models, like fields that are allowed to be inserted into the model database table, delete, or update. Additionally, it also supports flagging the model as read-only which limits the model to only perform select statements.



Properties

table

Model table name name.

protected string $table 

primaryKey

The default primary key column name for table, this will be used while selecting, updating and more.

protected string $primaryKey

searchable

Define your database table searchable columns, the fields that search will have to look for queries.

protected array<int,string> $searchable

cacheable

Enable or disable database caching for model while calling select, find, search, total or count.

protected bool $cacheable

expiry

Database cache expiration time TTL in seconds.

protected DateTimeInterface|int $expiry

readOnly

Specify whether the model's table is updatable, deletable, and insertable.

protected bool $readOnly

insertable

Define fields that can be inserted into.

protected array $insertable

updatable

Define fields that can be updated.

protected array $updatable

rules

Define your Input validation rules based on Validator class rules.

protected array<string,string> $rules

messages

Define your Input validation rules errors messages.

protected array<string,array> $messages

builder

Database query builder class instance.

protected \Luminova\Database\Builder $builder

validation

Input validation class instance.

protected static \Luminova\Security\Validation $validation

Methods

constructor

Constructor for the Model class, if query builder instance is passed null, it will create a new instance.To use with your configured builder instance pass it.

public __construct(?\Luminova\Database\Builder $builder = null): mixed

Parameters:

ParameterTypeDescription
$buildernull|\Luminova\Database\BuilderQuery builder class instance.

insert

Insert a new record into the current database.

public insert(array<string,mixed> $values): bool

Parameters:

ParameterTypeDescription
$valuesarray<string,mixed>nested array of values to insert into table.

Return Value:

bool - Return true if records was inserted, otherwise false.

Throw Exception:

\Luminova\Exceptions\RuntimeException - Throws if insert columns contains column names that isn't defined in $insertable.


update

Update current record in the database.

public update(array<int,mixed>|string|float|int|null $key, array $data, int $max = 1): bool

Parameters:

ParameterTypeDescription
$keystring|float|int|array<int,mixed>|nullThe key?s to update its record or null to update all records in table.
$dataarrayAn associative array of columns and values to update.
$maxintThe maximum number of records to update.

Return Value:

bool - Return true if records was updated, otherwise false.

Throw Exception:

\Luminova\Exceptions\RuntimeException - Throws if update columns contains column names that isn't defined in $updatable.


find

Fine next or a single record from the database table.

public find(array<int,mixed>|string|float|int $key, array<int,string> $fields = ['*']): mixed

Parameters:

ParameterTypeDescription
$keystring|array<int,mixed>|float|intThe key?s to find its record
$fieldsarray<int,string>The fields to retrieve (default is all).

Return Value:

mixed - Returns selected records or false on failure.


select

Select records from the database table.

public select(
    string|int|float|array<int,mixed>|null $key = null, 
    array<int,string> $fields = ['*'], 
    int $limit = 100, 
    int $offset = 0
): mixed

Parameters:

ParameterTypeDescription
$keystring|array<int,mixed>|float|int|nullThe key?s to select its record, if null all record in table will be selected.
$fieldsarray<int,string>The fields to retrieve (default is all).
$limitintSelect result limit (default: 100).
$offsetintSelect limit offset (default: 0).

Return Value:

mixed - Returns selected records or false on failure.


Select records from the database.

public search(string $query, array<int,string> $fields = ['*'], int $limit = 100, int $offset = 0): mixed

Parameters:

ParameterTypeDescription
$querystringSearch query string, escape string before passing.
$fieldsarray<int,string>The fields to retrieve (default is all).
$limitintSearch result limit (default: 100).
$offsetintSearch limit offset (default: 0).

Return Value:

mixed - Returns found records or false on failure.


delete

Delete a record from the database.

public delete(string|array<int,mixed>|float|int|null $key = null, int $max = 1): bool

Parameters:

ParameterTypeDescription
$keystring|array<int,mixed>|float|int|nullThe keys to delete, if null all record in table will be deleted.
$maxintThe maximum number of records to delete.

Return Value:

bool - Return true if the record was successfully deleted otherwise false.


total

Get total number of records in the database.

public total(): int|bool 

Return Value:

int|bool - Return the number of records or false if failed.


count

Get total number of records in the database based on the keys.

public count(array<int,mixed>|string|float|int $key): int|bool 

Parameters:

ParameterTypeDescription
$keyarray<int,mixed>|string|float|intThe key?s to find total number of matched.

Return Value:

int|bool - Return the number of records or false if failed.


purge

Deletes all cache entries related to the current model.

This method removes all cache files for the model from the cache directory. The path is constructed based on the model's class name and is expected to be within the filesystem cache directory.

public purge(): bool

Return Value:

bool - Returns true if the cache files are successfully deleted, false otherwise.


validation

Initialize and ser validation class object.

protected validation(): Luminova\Security\Validation

Return Value:

Luminova\Security\Validation - Return the number of records.

After first initialization you can then use static::$validation to access the object.


doSearch

Run a search in database table using the $searchable to index search columns. This method uses third-party libraries to search database table.

public doSearch(
    string $query, 
    array $fields = ['*'], 
    int $limit = 100, 
    int $offset = 0, 
    string $flag = 'any'
): mixed

Parameters:

ParameterTypeDescription
$querystringsearch query string, escape string before passing.
$fieldsarray<int,string>The fields to retrieve (default is all).
$limitintSearch result limit (default: 100).
$offsetintSearch limit offset (default: 0).
$flagstringSearch matching flag, default is (any) any matching keyword.

Return Value:

mixed - Return found records or false on failure.

Throws:

To use this method or searchInstance you will first have to install a third party library by running composer command composer require peterujah/php-search-controller .


searchInstance

Return search controller class instance.

protected searchInstance(string $flag): \Peterujah\NanoBlock\SearchInstance

Parameters:

ParameterTypeDescription
$flagstringSearch matching flag (e.g any, start).

Return Value:

SearchInstance - Search controller instance.

Throws:

Search Flags

The $flags keys to predefined search behaviors in the SearchInstance class. Here are the available flags and their corresponding behaviors:

  • 'start': Matches records that start with the query string.
  • 'end': Matches records that end with the query string.
  • 'any': Matches records that contain the query string anywhere within the field.
  • 'second': Matches records that contain the query string in the second position (specific use case).
  • 'length2': Matches records that start with a query string of length 2.
  • 'length3': Matches records that start with a query string of length 3.
  • 'startend': Matches records that start and end with the query string.

getTable

Get the name of the database table associated with this model.

public getTable(): string

Return Value:

string - Return the name of the database table.


getKey

Get the primary key field name for this model.

public getKey(): string

Return Value:

string - Return the primary key field name.


getSearchable

Get the table searchable array of column names.

public getSearchable(): array<int,string>

Return Value:

array<int, string> - Return table searchable column names.


getConn

Get instance of database connection.

public getConn(): Luminova\Interface\DatabaseInterface

Return Value:

DatabaseInterface - Return database driver connection instance.

Throws:


getBuilder

Get instance of database builder class.

public getBuilder(): ?Luminova\Database\Builder

Return Value:

Builder|null - Return the instance database builder.


getLastInsertedId

Retrieve last inserted id from database after insert method is called.

public getLastInsertedId(): mixed

Return Value:

mixed - Return last inserted id from database.


setReturn

Change the database result return type (e.g, array or object).

public setReturn(string $returns): self

Parameters:

ParameterTypeDescription
$returnsstringThe result returned as (e.g, array or object).

Return Value:

self - Return instance of model class.


isReadOnly

Check if method is read only

public isReadOnly(): bool

Return Value:

bool - Return true if is read only otherwise false.