LiquidoORM
LiquidoORM is a simple object relational mapping which helps you query database easily with just a minimum amount of code., (*1)
How to Install
composer require hidejec/liquido-orm "0.0.2"
then autoload in your project, (*2)
require 'vendor/autoload.php';
Configuration
Create a configuration file for your database and name it dbconfig.liquido.php, (*3)
# File name: dbconfig.liquido.php
And save it inside your project root directory., (*4)
Intialize the LiquidoORM in your index.php, (*5)
new Liquido\App;
Usage
Create a model and extend the LiquidoORM. For example a Customer Model, (*6)
#File: app/Model/Customer.php
namespace Model;
use Liquido\Model\LiquidoORM; #Requirement!
class Customer extends LiquidoORM{ #Requirement!
}
The Model automatically set the table with the plural name of the Model so you don't have to write again and again the table you want to run the query., (*7)
In our example, The table is set to customers
. So be sure that you have a table named customers
inside your database.
Another example: if you have a model class name Product
, the table will be set to products
automatically.
Another example: Class Name Illness
= illnesses
., (*8)
If you want to specify custom table name, just add this inside your model:, (*9)
protected static $table = "tablename";
Note that it should be protected static $table
Get all result and store it in a variable, (*10)
$list = Customer::all();
Simple right ? :)
It returns an array of results which you can manipulate inside a loop., (*11)
Example if you are using a twig view
{% for customer in list %}
{{ customer.Email|e }}
#or
{{ customer["Email"] }}
{% endfor %}
Get a single result passing a primary key ID, (*12)
$customer = Customer::withId(1);
echo $customer["Username"]; # Prints the username of the customer
Get result with predefined conditions, (*13)
Example I will fetch all data with the first name "Jacob". To do this...
$list = Customer::with([
'First_Name' => 'Jacob'
]);
Fetching with multiple conditions
$list = Customer::with([
'Status'=> 'Single',
'First_Name' => 'Jacob',
'condition-type'=> 'AND'
]);
This will going to select all rows with a Status "Single" AND First_Name "Jacob"., (*14)
condition-type can be a
- AND
- &&
- OR
, (*15)
How about an OR inside a condition-type AND., (*16)
Example Select all rows with a Status "Single" AND First_Name "Jacob" AND (Last_Name "Ramos" OR "Reyes") To do this..., (*17)
$list = Customer::with([
'Status'=> 'Single',
'First_Name' => 'Jacob',
'Last_Name' => ['Ramos', 'Reyes'],
'condition-type'=> 'AND'
]);
The 'Last_Name' => ['Ramos', 'Reyes']
Automatically pertains to the conditional statement "OR". So in normal query it will be WHERE Last_Name = 'Ramos' OR Last_Name = 'Reyes';, (*18)
I prefer the above method if your going the search for a data with a specified string. But what if your going to fetch the data with the id's less than 10., (*19)
Get data for numerical conditions, (*20)
Note that you can also use this similar to the above ::with() but I prefer to use the liquid method ::where() if you have a condition related to numbers since it's much easy to use., (*21)
$list = Customer::where("id", "<", "10");
How to Add/Insert a data, (*22)
$list = Customer::add([
'Username' => 'Jacob123',
'First_Name' => 'Jacob',
'Last_Name' => 'Ramos',
'Status' => 'Single',
'Email' => 'jacob123@yahoo.com'
]);
You can pass any amount of columns depending on your needs., (*23)
Tip
Optionally you can specify a column name to the queries., (*24)
The default was set to all. SELECT * FROM table;, (*25)
To query to a specific column example the liquid method ::withId(), (*26)
$customer = Customer::withId(1, "Email"); # Single Column
$customer = Customer::withId(1, "Email, Username, First_Name"); # You can also specify multiple column names
Note that this is applicable to all LIQUID METHODS just add another argument prior to the required arguments of the liquid methods., (*27)
NOTE THAT THIS IS A PRE_RELEASED!, (*28)
Further improvements and functionalities will be released in the future builds.
Thank you :), (*29)
Cheers,, (*30)
Hidejec - Developer of Team Liquido, (*31)