Download the PHP package jijihohococo/ichi-orm without Composer
On this page you can find all versions of the php package jijihohococo/ichi-orm. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download jijihohococo/ichi-orm
More information about jijihohococo/ichi-orm
Files in jijihohococo/ichi-orm
Informations about the package ichi-orm
Ichi ORM
Ichi ORM is aimed to be the fast performance and secure database ORM for PHP with simple usage.
License
This package is Open Source According to MIT license
Table of Contents
- Ichi ORM
- License
- Table of Contents
- Installation
- Set up Database Connection
- Available Database Setting
- Table Structure
- Create Model From Commandline
- Configuration Table Name
- Configuration Primary Key
- CRUD
- Create
- Disable Auto increment Id
- Insert Multiple Rows In One Query
- Retrieve
- Refers To
- Refers Many
- Update
- Update Multiple Rows In One Query
- Delete
- Create
- Querying
- SELECT
- Getting Query Data
- Get
- To Array
- Get Query Data With Soft Deleted Data
- LIMIT
- WHERE
- OR WHERE
- WHERE IN
- WHERE NOT IN
- Join
- Inner Join
- Left Join
- Right Join
- Union
- Pagination
- Database Pagination
- Array Pagination
- Subqueries
- Using PDO Functions
- Using Different Databases
- JSON Response
- Caching
- Observers
Installation
Set up Database Connection
This library can connect MySQL, Postgres and MS SQL Server.
Firstly, you need to declare your database driver like below.
If you want to add another custom database connection, you can do just like that.
You must add dbname,host,user_name and user_password in your database connection. I recomend you to use "utf8mb4" for your database charset and "utf8mb4_unicode_ci" for your database collation.
In defalt database connections, you don't need to add driver parameters but in your custom database connection you have to add driver parameters.
Default database connections are 'mysql' , 'pgsql' and 'sqlsrv'.
Supported database drivers are 'mysql' , 'pgsql' and 'sqlsrv'.
After declaring database connection, you can select default database connection
Available Database Setting
Name | Description | Required |
---|---|---|
driver | Database driver name | ✓ |
dbname | Database name | ✓ |
charset | Charset Font | |
collation | Collation Font Setting for MySQL and Postgres SQL | |
host | Database Host Address | ✓ |
user_name | Database User Name | ✓ |
user_password | Database User Password | ✓ |
unix_socket | Unix Socket For MySQL | |
port | Databse Port Number | |
strict (bool) | Strict Mode In MySQL | |
time_zone | Database Time Zone in MySQL and Postgres SQL | |
isolation_level | To set Isolation Level in MySQL | |
modes (array) | To set sql_mode in MySQL | |
synchronous_commit | To set Synchronous Commit in Postgres SQL | |
sslmode | To set SSL Mode in Postgres SQL | |
sslcert | To set SSL Certificate in Postgres SQL | |
sslkey | To set SSL Key in Postgres SQL | |
sslrootcert | To set SSL Root Certificate in Postgres SQL | |
readOnly (bool) | True To set ApplicationIntent to ReadOnly in MS SQL Server | |
pooling (bool) | True To set ConnectionPooling to true in MS SQL Server | |
application_name | To set APP in MS SQL Server OR application name in Postgres SQL | |
encrypt | To set ENCRYPT in MS SQL Server | |
trust_server_certificate | To set TrustServerCertificate in MS SQL Server | |
multiple_active_result_sets | To set MultipleActiveResultSets in MS SQL Server | |
transaction_isolation | To set TransactionIsolation in MS SQL Server | |
multi_subnet_failover | To set MultiSubnetFailover in MS SQL Server | |
column_encryption | To set ColumnEncryption in MS SQL Server | |
key_store_authentication | To set KeyStoreAuthentication in MS SQL Server | |
key_store_principal_id | TO set KeyStorePrincipalId in MS SQL Server | |
key_store_secret | To set KeyStoreSecret in MS SQL Server | |
login_timeout | To set LoginTimeout in MS SQL Server |
Table Structure
If you have the column named "deleted_at", be sure that the column is NULLABLE column.
Create Model From Commandline
Firstly you need to created the file named "ichi" under your project folder and use the below code in this file
And then you can create the model in your commandline
The default file folder is "app/Models". So after making command, the model you created will be in the this default file folder. If you want to change the default folder path, you can change it in your "ichi" file.
Configuration Table Name
In Ichi ORM, one model class which is extended "JiJiHoHoCoCo\IchiORM\Database\Model" abstract class is represented one table.
In default, the table name of the model class will show according to the format below
Model | Table |
---|---|
Item | items |
OrderItem | order_items |
If the above format is not suitable for the model class, you can customize in your model class
Configuration Primary Key
In default, the primary key for the table is represented "id". If you want to change that, you can customize in your model class
CRUD
Firstly, you need to extend Model Class from your class and declare your data fields as attributes in your model as shown as below.
Create
You can create the data as shown as below.
It is your choice to add or not to add the nullable field data into array in "create" function.
If you have "created_at" data field, you don't need to add any data for that data field. Ichi ORM will automatically insert current date time for this data field. The data field must be in the format of timestamp or varchar.
You can get the new model object after creating.
App\Models\Blog Object ( [id] => 1 [author_id] => 1 [content] => Content [created_at] => 2021-10-01 12:02:26 [updated_at] => [deleted_at] => )
Disable Auto increment Id
If you don't use auto increment id in your table you must write this function in your model class
And you must add your ID Values from your side manually like this
Insert Multiple Rows In One Query
If you want to insert multiple rows in one query you can do according to below coding flow.
Retrieve
You can get your data by your primary key as shown as below.
If you don't want to get your data by your primary key, you can do as shown as below.
First Parameter is field name and second parameter is value.
You can get only single object by using "find" and findBy" function.
Refers To
If you have one to one relationship in your database (with foreign keys or without foreign keys), you can use "refersTo" function in child model class as shown as below. The function will output the single object.
You must add parent model name, the field that represent parent id into "refersTo" function if parent model's primary key is "id".
You must add parent model name, the field name that represent parent id and parent primary key field into "refersTo" function if parent model's primary key is not "id".
You can get parent data as single object in your controller or class.
You don't need to worry about null. It has null safety.
Refers Many
If you have one to many relationship in your database (with foreign keys or without foreign keys), you can use "refersMany" function in parent model class as shown as below. The function will output the object array.
You must add child model name and the field name that represent parent id in child model into "refersMany" function if parent model's primary key is "id".
You must add child model name, the field name that represent parent id in child model and parent primary key field into "refersMany" function if parent model's primary key is not "id".
You can customize the child query
You can get child data as object array in your controller or class.
Update
You can update your data as shown as below.
You can get the model object after updating
If you have "updated_at" data field, you don't need to add any data for that data field. Ichi ORM will automatically insert current date time for this data field. The data field must be in the format of timestamp or varchar.
App\Models\Blog Object ( [id] => 1 [author_id] => 1 [content] => New Content [created_at] => 2021-10-01 12:02:26 [updated_at] => 2021-10-01 12:03:26 [deleted_at] => )
Update Multiple Rows In One Query
If you want to update multiple rows in one query you can do according to below coding flow.
Delete
You can delete your data as shown as below.
If you have "deleted_at" data field and "deleted_at" data field is nullable, you have soft delete function. So, the data will not actually delete after deleting but this data will not be shown in querying in default.
Soft Delete Functions can't be used if you don't have "delete_at" data field and the data will be deleted.
If you want to restore your soft deleted data, you can do as shown as before.
If you want to force to delete your data (whatever it is able to be soft deleted or not), you can do as shown as before.
Querying
SELECT
To make "SELECT" sql query, you can use "select" function as shown as below
Getting Query Data
You can get your query data with "get()" and "toArray()" functions.
Get
"get()" function can use in main query and subquery. This function will return the object array of related model when it is used in main query as shown as below.
Array ( [0] => App\Models\Blog Object ( [id] => 1 [author_id] => 1 [content] => Content [created_at] => 2021-10-01 12:02:26 [updated_at] => 2021-10-01 12:02:26 [deleted_at] => ) )
You can call relationship functions directly with the object in the loop because "get()" function outputs the object array
If you don't use select function, you will get all data fields of related model.
To Array
"toArray()" function can use in only main query. This function will return the array for thre query as shown as below.
Array ( [0] => Array ( [id] => 1 [author_id] => 1 [content] => Content [created_at] => 2021-10-01 12:02:26 [updated_at] => 2021-10-01 12:02:26 [deleted_at] => ) )
You can't call relationship functions directly with the object in the loop because "toArray()" function outputs the array.
You can't use "toArray" function in subquery.
If you don't use select function, you will get all data fields of related model.
Get Query Data With Soft Deleted Data
If you have soft deleted data rows, you can't see those in your array or data object array. If you want to see the array or data object array with soft deleted data rows, you must use "withTrashed()" function as shown as below.
If you don't use select function, you will get all data fields of related model. You will also get soft deleted data rows if you use "withTrashed()" function.
LIMIT
To make limit sql query, you can use "limit" function and put the integer into this function as shown as below
In main query
In subquery
WHERE
To make "WHERE" sql query, you can use "where" function as shown as below
In case of '='
If you want to add operators
OR WHERE
To make "OR WHERE" sql query, you can use "orWhere" function as shown as below
In case of '='
If you want to add operators
WHERE IN
To make "WHERE IN" sql query, you can use "whereIn" function as shown as below
WHERE NOT IN
To make "WHERE NOT IN" sql query, you can use "whereNotIn" function as shown as below
Join
The rules and flows are same as SQL Join.
Inner Join
Single SQL Query
Subquery
Left Join
Single SQL Query
Subquery
Right Join
Single SQL Query
Subquery
Union
You can use "union" function in queries.
You can use "union" function in subqueries.
Pagination
In this library, you can use two types of pagination.
- Database Pagination
- Array Pagination
The default paginated data per page is 10. You can customize that number. Pagination functions will output the array according to the below format. So, you can use server pagination into your frontend (like Vue and React) with that array data.
Database Pagination
You can paginate your query result like that
You can customize the number of paginated data by
You can get paginated data like below. The data in "data" array key is object array.
You can call relationship functions directly with the object in the loop.
You can use pagination user interface in your frontend php file like
You can customize the pagination user interface color
Array Pagination
You can paginate your array like below.
You can also use multidimensional array
You can customize the number of paginated data by
You can use pagination user interface in your frontend php file like
You can customize the pagination user interface color
Subqueries
If you want to use subquery within one table you can do as shown as before.
You can use subqueries as shown as below in "where","orWhere" and "whereIn" functions.
If you want to use subquery from different table you can do as shown as before.
You can use "from" function in only subqueries. You need to add model class name which is represented the another table in "from" function.
If you want to use subquery in select, you can use "addSelect" and "addOnlySelect" functions.
"addSelect" function is making subquery in select query. It will select the data within its function with the data from "select" function. If you don't use "select" function, it will select the data within its function with the data of all fields' values of selected table.
You can't use "addSelect" function in subqueries
"addOnlySelect" function is making subquery in select query. It will select only the data within its function. You can't use other select functions("select" and "addSelect") if you want to use "addOnlySelect" function.
You can use "addOnlySelect" function in subqueries
Using PDO Functions
You can use PDO functions like that. You can use all PDO functions according to https://www.php.net/manual/en/class.pdo.php
If you want to use default database connection with PDO object
If you want to use selected database connection with PDO object
Using Different Databases
If you have the model which is from different database you can connect like that
JSON Response
When you want to do json data of for your API you can simply do as shown as below.
You can customize http response code for json response. Default http response code is 200.
If you want to customize your JSON data, firstly you need to create the class.
You must extend "JiJiHoHoCoCo\IchiORM\Resource\ResourceCollection" abstract class and declare "getSelectedResource()" function for your all resource collection classes.
You can create the resource class via terminal after creating "ichi" file as we mentioned in Create Model From Commandline
The default path for observer is "app/Resources". You can also change this in "ichi" file.
And then, you can do to show to your custom JSON Resource as shown as below.
For Object Array-
For Single Object-
You can declare your relationship in your resource collection class (For refers to and refers many).
You can declare another resource collection (according to the data is single object or object array) in your resource collection class.
Caching
You can cache your query data with redis or memcached extensions in this library.
Firstly, you need to pass the object of redis or memcached into the "JiJiHoHoCoCo\IchiORM\Cache\CacheModel" static function "setCacheObject" like below.
With Redis
With Memcached
It might be different of connecting the way of redis or memcached to each other according to the security and ports' availabilities. The important thing is you must pass the redis or memcached object into the "setCacheObject" static function of "JiJiHoHoCoCo\IchiORM\Cache\CacheModel".
And then, you can call the cache functions to store and get.
In "remember" function you must declare the cached key name,and the stored query or data and expired time in seconds. Without adding expired time is also ok but it will save the data into the unlimited time. This function will store the data if the declared cached key is not in the cached server and get the cached data if the declared cached key is in the cached server.
The default stored time is unlimited. So you must declare the stored time for your cached server
If you want to delete your cached key, you can do
You can just save your data in your cache
To get your cached data
You can get back your redis object to implement the functions of redis extension.
You can also get back your memcached object to implement the functions of memcached.
Observers
To make observers firstly you need to create the observer class which implements "JiJiHoHoCoCo\IchiORM\Observer\ModelObserver" interface.
In this created class, you must declare the functions as shown as below.
- "create" function will load after creating the data of blog model.
- "update" function will load after updating the data of blog model.
- "delete" function will load after deleting the data of blog model.
- "restore" function will load after restoring the soft deleted data of blog model.
- "forceDelete" function will load after force deleting the data of blog model.
You can create the observer via terminal after creating "ichi" file as we mentioned in Create Model From Commandline
The default path for observer is "app/Observers". You can also change this in "ichi" file.
After creating observer, you must do
You can also add many observers for one model
The observers' functions will load sequetly.
If you want to observe your custom function
In model
In observer
If you need to pass multiple parameters in observer function.
In model
In observer