Skip to content

Simple Object Relational Mapper for manipulate data with fluent interface and TDD approach.

License

Notifications You must be signed in to change notification settings

Mohammadreza-73/Astro-ORM

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

astro-orm-banner

Total Downloads Latest Stable Version StyleCI License PHPStan

object-relational mapper (ORM), lets you query and manipulate data with fluent api from a database using an object-oriented paradigm.

πŸ“Œ Requirements

  • PHP >= 7.2
  • PDO Extension

⬇️ Installation

You can install the package via the composer:

composer require m.rahimi/astro-orm

πŸ‘€ How its works

UML

1. Setup your database configs

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.

2. Implements Connection Contract

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

3. Use Query Builder

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

Insert Data: return last insert id

$data = [
    'name'  => 'John',
    'email' => 'john.doe@gmail.com',
    'link'  => 'https://example.com',
    'skill' => 'PHP'
];

$last_id = PDOQueryBuilder::table('users')->create($data);

update

Update Data: return true if successful

$result = PDOQueryBuilder::table('users')
    ->where('name', 'John')
    ->where('skill', 'PHP')
    ->update([
        'skill' => 'Javascript',
        'name' => 'Jeff',
        'email' => 'jeff@gmail.com'
    ]);

Multiple where

$result = PDOQueryBuilder::table('users')
    ->where('name', 'John')
    ->where('skill', 'JS')
    ->update(['skill' => 'Javascript']);

Multiple orWhere

$result = PDOQueryBuilder::table('users')
    ->orWhere('skill', 'PHP')
    ->orWhere('skill', 'JS')
    ->get();

Delete

Delete Data: return true if successful

$result = PDOQueryBuilder::table('users')
    ->where('name', 'John')
    ->delete();

Fetch

$result = PDOQueryBuilder::table('users')
    ->where('name', 'John')
    ->where('skill', 'Javascript')
    ->get();

Fetch first row

$result = PDOQueryBuilder::table('users')
    ->where('name', 'First Row')
    ->first();

Fetch first row or throw exception on failure

$result = PDOQueryBuilder::table('users')
    ->where('name', 'Jim')
    ->firstOrFail();

Find ID

$result = PDOQueryBuilder::table('users')
    ->find($id);

Find ID or throw exception on failure

$result = PDOQueryBuilder::table('users')
    ->findOrFail($id);

Find with value

$result = PDOQueryBuilder::table('users')
    ->findBy('name', 'Jack');

Get specific rows

$result = PDOQueryBuilder::table('users')
    ->where('name', 'Jack')
    ->limit(5)
    ->get();

Sort rows

$result = PDOQueryBuilder::table('users')
    ->orderBy('skill', 'DESC')
    ->get();

Testing

Run the tests with:

composer test

Contributing

Contributions are welcome! To contribute, please familiarize yourself with CONTRIBUTE.md

Security

If you discover any security related issues, please email mohammadreza.rahimi1373@gmail.com instead of using the issue tracker.

License

The MIT License (MIT). Please see License File for more information.