Download the PHP package iqomp/model without Composer
On this page you can find all versions of the php package iqomp/model. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package model
Short Description Generalization database model for standardize usage
License MIT
Informations about the package model
iqomp/model
The other way to manage database with style. The main purpose of this module is to create a way to interact with database without handle connection and the manegement it self. This is other way to communicate with database other than the way hyperf use it.
Installation
Publishing Config
Configuration
The configurasion is saved at config/autoload/model.php
that contain which model
connection type to use for some model. The database connection it self is return
back to hyperf style.
drivers
All known drivers. Most of the time, this property registered by module.
models
Configuration to decide database connection to use for each models. If the model
is not registered here, it will use default
connection if exists, or throw an
ConnectionNotFoundException
exception. The model name accept wildcard ( *
)
that match any part of the model name. Each model should have property read
and
write
that define connection for read and write.
Model Structure
Each model class should extends from Iqomp\Model\Model
and has at least one
public static property named table
. Below is mostly how model look like:
Drivers
Driver is the one that manage connection to database and communication with the
database. Each driver should register it self as model driver with content config
as file config/autoload/model.php
as below:
Driver class should implement interface Iqomp\Model\DriverInterface
. Below are
list of method that should be implemented by the driver:
All of above method is public
method.
Usage
After creating the model, it's now easy to use it from app:
Sorting
Method get
and getOne
has $order
argument that can be used to sort the result.
The value is an array field-order pair where field
is column name of the table,
and order
is boolean false
for descending, and true
is ascending.
Where Condition
Some method accept argument $where
that filter the action execution. This part
explain where usage:
Standard
The most simple way to create a where condition is as below:
IN Query
Combain multiple filter value in an array to use them in in
operator:
Operator
To use other than =
for operator comparation, use below style:
Known operator so far are >
, <
, <=
, >=
, !=
, and NOT IN
.
BETWEEN
To use between operator, use it as below:
LIKE
Array prefix __like
can be used to use LIKE
operator:
AND
By default, each array where is combined with AND
operator. If your where condition
is not that standard, you can use $and
array key to combine each of sub array
combined with AND
:
OR
Just like $and
, you can also use OR
to combine each condition with $or
array
key:
Unescape
If you need to unescape column name on where condition, add prefix ?
to the
column name:
Validator
If your application use iqomp/validator for you object validator, this module add new validator that can be used to validate object that related to data model.
unique
Make sure the data is not yet on database:
If there're multiple column to be test, set field
property value as array:
exists
Make sure the data is in database:
exists-list
Make sure all values of object property ( which is array ) already exists in database:
Formatter
If your application use iqomp/formatter for your object formatter, this module add a few format type.
By default, no data will taken from database, instead all property with below
format type will use null
or {"id": value}
as the object value.
If you need to fill the property with data from the table, add additional options on calling your formatter:
By calling above function, options.user = true
tell the handler to take the data
from database.
In case object user
has sub-object that you want to fetch from database, use
array key-bool pair as the value of options user:
Above action will take property user
of current object from database, then take
property profile
from database of user
property.
If you need to add additional where condition while fetching the data, you can also add it on the options:
Above action will get data from database with additional where condition status = 1
.
Format Types
multiple-object
Explode the object property that separate by some string or using json_decode and convert them to be objects that taken from database:
chain
Take objects from other table that joined by another table.
object
Convert the value of object property to be object data that taken from table:
object-switch
Switch model usage based on other object property value:
partial
Take object from other table with relation id is as current object id. This action will add new object property
Additional Properties
Each format accept additional config named field
, fields
, or format
. There
can only be one of them in a single property config.
field
After getting the data from database, take only one column ( which is field->name
)
from the result and use the value as current object property. If property type
exists, apply that format type to the taken value.
fields
Just like field
accept this method get multiple data from database result and
use all mentioned property as new value of current object property. If property
type
exists, apply that format type to the taken value.
format
Format the data taken from database with this format name, and use the formatted data as current object property.