Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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.
- Host: GitHub
- URL: https://github.com/mohammadreza-73/astro-orm
- Owner: Mohammadreza-73
- License: mit
- Created: 2021-11-29T13:43:27.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2024-01-13T21:02:37.000Z (12 months ago)
- Last Synced: 2024-11-30T19:17:34.902Z (about 1 month ago)
- Topics: database, fluent-api, mysql-orm, orm, pdo-php, php-orm, sql, tdd
- Language: PHP
- Homepage:
- Size: 188 KB
- Stars: 3
- Watchers: 1
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
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
### 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.