object-relational mapper (ORM), lets you query and manipulate data with fluent api from a database using an object-oriented paradigm.
- PHP >= 7.2
- PDO Extension
You can install the package via the composer:
composer require m.rahimi/astro-orm
Fill the config/database.php
file with your database configuration.
NOTE: You Can use any database extensions, like: PDO, Mysqli, Sqlite,etc. Just define its array key.
In this project i use PDO driver. so i create Database/PDODatabaseConnection.php
file and implements contracts methods.
connect(); // Which implements database connection
getConnection(); // Which return database connection
Now get database configs, create new instanse of PDODatabaseConnection
and connect to DB.
$this->config = $this->getConfigs('database', 'astro_orm');
$pdoConnection = new PDODatabaseConnection($this->config);
$pdoHandler = $pdoConnection->connect();
Insert Data: return last insert id
$data = [
'name' => 'John',
'email' => '[email protected]',
'link' => 'https://example.com',
'skill' => 'PHP'
];
$last_id = PDOQueryBuilder::table('users')->create($data);
Update Data: return true if successful
$result = PDOQueryBuilder::table('users')
->where('name', 'John')
->where('skill', 'PHP')
->update([
'skill' => 'Javascript',
'name' => 'Jeff',
'email' => '[email protected]'
]);
$result = PDOQueryBuilder::table('users')
->where('name', 'John')
->where('skill', 'JS')
->update(['skill' => 'Javascript']);
$result = PDOQueryBuilder::table('users')
->orWhere('skill', 'PHP')
->orWhere('skill', 'JS')
->get();
Delete Data: return true if successful
$result = PDOQueryBuilder::table('users')
->where('name', 'John')
->delete();
$result = PDOQueryBuilder::table('users')
->where('name', 'John')
->where('skill', 'Javascript')
->get();
$result = PDOQueryBuilder::table('users')
->where('name', 'First Row')
->first();
$result = PDOQueryBuilder::table('users')
->where('name', 'Jim')
->firstOrFail();
$result = PDOQueryBuilder::table('users')
->find($id);
$result = PDOQueryBuilder::table('users')
->findOrFail($id);
$result = PDOQueryBuilder::table('users')
->findBy('name', 'Jack');
$result = PDOQueryBuilder::table('users')
->where('name', 'Jack')
->limit(5)
->get();
$result = PDOQueryBuilder::table('users')
->orderBy('skill', 'DESC')
->get();
Run the tests with:
composer test
Contributions are welcome! To contribute, please familiarize yourself with CONTRIBUTE.md
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.