hidejec / liquido-orm
LiquidoORM Framework Developed by Team Liquido(Inspired by Laravel Eloquent ORM)
Requires
- php: >=5.3.0
This package is not auto-updated.
Last update: 2025-02-01 21:19:51 UTC
README
LiquidoORM is a simple object relational mapping which helps you query database easily with just a minimum amount of code.
How to Install
composer require hidejec/liquido-orm "0.0.2"
then autoload in your project
require 'vendor/autoload.php';
Configuration
Create a configuration file for your database and name it dbconfig.liquido.php
# File name: dbconfig.liquido.php <?php define( "DB_DSN", "mysql:host=localhost;dbname=Database_Name" ); define( "DB_USERNAME", "Database_Username" ); define( "DB_PASSWORD", "Database_Password" ); ?>
And save it inside your project root directory.
Intialize the LiquidoORM in your index.php
new Liquido\App;
Usage
Create a model and extend the LiquidoORM. For example a Customer Model
#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.
In our example, The table is set to
customers
. So be sure that you have a table namedcustomers
inside your database. Another example: if you have a model class nameProduct
, the table will be set toproducts
automatically. Another example: Class NameIllness
=illnesses
.
If you want to specify custom table name, just add this inside your model:
protected static $table = "tablename";
Note that it should be protected static $table
Get all result and store it in a variable
$list = Customer::all();
Simple right ? :) It returns an array of results which you can manipulate inside a loop.
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
$customer = Customer::withId(1); echo $customer["Username"]; # Prints the username of the customer
Get result with predefined conditions
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".
condition-type can be a
- AND
- &&
- OR
- ||
How about an OR inside a condition-type AND.
Example Select all rows with a Status "Single" AND First_Name "Jacob" AND (Last_Name "Ramos" OR "Reyes") To do this...
$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';
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.
Get data for numerical conditions
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.
$list = Customer::where("id", "<", "10");
How to Add/Insert a data
$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.
Tip
Optionally you can specify a column name to the queries.
The default was set to all. SELECT * FROM table;
To query to a specific column example the liquid method ::withId()
$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.
NOTE THAT THIS IS A PRE_RELEASED!
Further improvements and functionalities will be released in the future builds. Thank you :)
Cheers,
Hidejec - Developer of Team Liquido