This is a light weight ORM for core PHP applications that supports MySQL, Postgres and Sqlite database engine out of the box.
Install PHP-ORM with composer
composer require octacondeveloper/php-orm
## MYSQL Connection Example
DB_CONNECTION=mysql
DB_HOST=
DB_PORT=
DB_DATABASE=
DB_USERNAME=
DB_PASSWORD=
# ## POSTGRES Connection Example
DB_CONNECTION=postgresql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=
DB_USERNAME=
DB_PASSWORD=
## SQLite Connection Example
DB_CONNECTION=sqlite
DB_PATH="test_db_two.sqlite"
use PhpOrm\Query;
$result = Query::table('users')->all();
Or You can use it as a Model Extension
use PhpOrm\Model;
class User extends Model {
//this is optional if your table name differs from the class name
protected $table = 'users';
}
Then in your class you can call the User Class with the Query methods
use `Your-class-namespace`
$result = User::all();
PHP-ORM provides you with two distinct methods of interactions with methods. Which can be through
This options allow to specify the table name directly while making the calls and it has several methods. A quick example of it usage is
use PhpOrm\Query;
$result = Query::table('users')->all();
Create a new record into the selected table
use PhpOrm\Query;
$data = [
"name" => "Jil Harrys",
"sex" => "Male"
];
$result = Query::table('users')->insert($data);
Insert bulk records at once
use PhpOrm\Query;
$data = [
[
"name" => "Jil Harrys",
"sex" => "Male"
],
[
"name" => "Lionel Messi",
"sex" => "Female"
],
[
"name" => "John Does",
"sex" => "Male"
],
];
$result = Query::table('users')->insertBulk($data);
Fetch all records on the table
use PhpOrm\Query;
$result = Query::table('users')->all();
Fetch the first record on the table
use PhpOrm\Query;
$result = Query::table('users')->first();
Fetch the last record on the datable, If a column is not provided, it would order it based on the primaryKey or based on the first availablecolumn on the table
use PhpOrm\Query;
$result = Query::table('users')->last();
$result = Query::table('users')->last('updated_at');
This allows you to pass direct sql string to the Query instance
use PhpOrm\Query;
$result = Query::rawQuery("SELECT * FROM users");
Add a query condition
use PhpOrm\Query;
$result = Query::table('users')->where('sex','harrys');
$result = Query::table('users')->where('sex',,'!=','harrys');
Add another compulsory query condition, this comes after an initial "where" method has been called
use PhpOrm\Query;
$result = Query::table('users')->andWhere('sex','harrys');
$result = Query::table('users')->andWhere('sex',,'!=','harrys');
Add another optional query condition, this comes after an initial "where" method has been called
use PhpOrm\Query;
$result = Query::table('users')->orWhere('sex','harrys');
$result = Query::table('users')->orWhere('sex',,'!=','harrys');
Order records based on column either in ASC or DESC. Default order is ASC
use PhpOrm\Query;
$result = Query::table('users')->orderBy('id','desc');
$result = Query::table('users')->orderBy('id');
Counts the number of records available on the table based on provided column and condition. If no parameters are passed, it count count the total records without any condition
use PhpOrm\Query;
$result = Query::table('users')->count();
$result = Query::table('users')->count('sex','male');
Get the maximum
use PhpOrm\Query;
$result = Query::table('users')->min('score');
Get the minimum
use PhpOrm\Query;
$result = Query::table('users')->min('score');
Update a record
use PhpOrm\Query;
$result = Query::table('users')->where('id','233')->update([
"sex" => "female"
]);
Delete a record
use PhpOrm\Query;
$result = Query::table('users')->where('id','233')->delete();
$result = Query::table('users')->delete();
Begin a transaction
use PhpOrm\Query;
$result = Query::table('users')->begingTransaction();
Commit a transaction
use PhpOrm\Query;
$result = Query::table('users')->commitTransaction();
Rollback a transaction
use PhpOrm\Query;
$result = Query::table('users')->rollbackTransaction();
Start a DB trabsaction session
use PhpOrm\Query;
$result = Query::table('users')->transaction(function(){
$result = Query::table('users')->where('id','233')->delete();
$result = Query::table('users')->delete();
});
Fetch a one to one model relationship between two models
use PhpOrm\Query;
$result = Query::table('users')->withOne('files','user_id','id');
Fetch a one to many model relationship between two models
use PhpOrm\Query;
$result = Query::table('users')->withOne('files','user_id','id');
The Model approach allows you to create a class for your table and extends the model base class. This allows you to have other custom SQL related transaction in your class. It can used as follows:
use PhpOrm\Model;
class User extends Model{
protected $table= 'users';
}
Defining the table variable is optional. By default the Package gets your class name and assumes the corresponding table name to it. Take for example
If the class name is "User", the assumption is that the table name is "users", same for "Student" and "students". But if this assumption is wrong, kindly set the value of the protected $table to override the default table name assumptions.
Create a new record into the selected table
$data = [
"name" => "Jil Harrys",
"sex" => "Male"
];
$result = User::insert($data);
Insert bulk records at once
$data = [
[
"name" => "Jil Harrys",
"sex" => "Male"
],
[
"name" => "Lionel Messi",
"sex" => "Female"
],
[
"name" => "John Does",
"sex" => "Male"
],
];
$result = User::insertBulk($data);
Fetch all records on the table
$result = User::all();
Fetch the first record on the table
$result = User::first();
Fetch the last record on the datable, If a column is not provided, it would order it based on the primaryKey or based on the first availablecolumn on the table
$result = User::last();
$result = User::last('updated_at');
Add a query condition
$result = User::where('sex','harrys');
$result = User::where('sex',,'!=','harrys');
Add another compulsory query condition, this comes after an initial "where" method has been called
$result = User::andWhere('sex','harrys');
$result = User::andWhere('sex',,'!=','harrys');
Add another optional query condition, this comes after an initial "where" method has been called
$result = User::orWhere('sex','harrys');
$result = User::orWhere('sex',,'!=','harrys');
Order records based on column either in ASC or DESC. Default order is ASC
$result = User::orderBy('id','desc');
$result = User::orderBy('id');
Counts the number of records available on the table based on provided column and condition. If no parameters are passed, it count count the total records without any condition
$result = User::count();
$result = User::count('sex','male');
Get the maximum
$result = User::min('score');
Get the minimum
$result = User::min('score');
Update a record
$result = User::where('id','233')->update([
"sex" => "female"
]);
Delete a record
$result = User::where('id','233')->delete();
$result = User::delete();
Fetch a one to one model relationship between two models
$result = User::withOne('files','user_id','id')->first();
$result = User::withOne('files','user_id','id')->all();
Fetch a one to many model relationship between two models
$result = User::withOne('files','user_id','id')->first();
$result = User::withOne('files','user_id','id')->all();
The package allows method chaining as most of the methods wont return a result until it's chained to certian methods.
use PhpOrm\Query;
$query = Query::table('cryptos')->where("name","Harrys")->orWhere("symbol","ETH")->orderBy("name", "asc")->take(2)->min("exchange_rate_dollar");
$query = Query::table('cryptos')->where("name","Harrys")->orWhere("symbol","ETH")->orderBy("name", "asc")->take(2)->first();
$query = Crypto::where("name","Harrys")->orWhere("symbol","ETH")->orderBy("name", "asc")->take(2)->min("exchange_rate_dollar");
$query = Crypto::where("name","Harrys")->orWhere("symbol","ETH")->orderBy("name", "asc")->take(2)->first();
Contributions are always welcome! This is an Open Source library and Pull Requests would be extremely welcome.