Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/mohammadreza-73/astro-orm

Simple Object Relational Mapper for manipulate data with fluent interface and TDD approach.
https://github.com/mohammadreza-73/astro-orm

database fluent-api mysql-orm orm pdo-php php-orm sql tdd

Last synced: 25 days ago
JSON representation

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

Awesome Lists containing this project

README

        

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.

```php
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.

```php
$this->config = $this->getConfigs('database', 'astro_orm');

$pdoConnection = new PDODatabaseConnection($this->config);
$pdoHandler = $pdoConnection->connect();
```

#### Insert

Insert Data: return last insert id

```php
$data = [
'name' => 'John',
'email' => '[email protected]',
'link' => 'https://example.com',
'skill' => 'PHP'
];

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

#### update

Update Data: return true if successful

```php
$result = PDOQueryBuilder::table('users')
->where('name', 'John')
->where('skill', 'PHP')
->update([
'skill' => 'Javascript',
'name' => 'Jeff',
'email' => '[email protected]'
]);
```

#### Multiple where

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

#### Multiple orWhere

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

#### Delete

Delete Data: return true if successful

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

#### Fetch

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

#### Fetch first row

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

#### Fetch first row or throw exception on failure

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

#### Find ID

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

#### Find ID or throw exception on failure

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

#### Find with value

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

#### Get specific rows

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

#### Sort rows

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

## Testing

Run the tests with:
```php
composer test
```

## Contributing

Contributions are welcome! To contribute, please familiarize yourself with [CONTRIBUTE.md](CONTRIBUTING.md)

## Security

If you discover any security related issues, please email [[email protected]](mailto:[email protected]) instead of using the issue tracker.

## License

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