Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/marcomilon/micro-db
Lightweight ORM library.
https://github.com/marcomilon/micro-db
activerecord composer database lightweight-orm-library mysql orm php query-builder
Last synced: 3 days ago
JSON representation
Lightweight ORM library.
- Host: GitHub
- URL: https://github.com/marcomilon/micro-db
- Owner: marcomilon
- License: mit
- Created: 2017-11-23T18:35:38.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2018-09-12T21:58:53.000Z (over 6 years ago)
- Last Synced: 2024-11-29T14:31:35.323Z (about 1 month ago)
- Topics: activerecord, composer, database, lightweight-orm-library, mysql, orm, php, query-builder
- Language: PHP
- Homepage:
- Size: 37.1 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Micro-db
[![Latest Stable Version](https://poser.pugx.org/fullstackpe/micro-db/v/stable)](https://packagist.org/packages/fullstackpe/micro-db) [![Build Status](https://travis-ci.org/marcomilon/micro-db.svg?branch=master)](https://travis-ci.org/marcomilon/micro-db)
Micro-db is a lightweight ORM library.
### Installation
First you need to install Composer. You may do so by following the instructions
at [getcomposer.org](https://getcomposer.org/download/).
Then run> composer require fullstackpe/micro-db
If you prefer you can create a composer.json in your project folder.
```json
{
"require": {
"fullstackpe/micro-db": "^1.1"
}
}
```Then run the command
> composer install
### The ActiveRecord Class
If you have a table called `book`. You need to create an active record Class
called `Book` that extends the Class `micro\db\ActiveRecord`. The class `Book`
needs to implement two methods: tableName() and dbConnection().#### Example
```php
use micro\db\ActiveRecord;class Book extends ActiveRecord {
public static function tableName()
{
return 'book';
}
public static function dbConnection()
{
$servername = "127.0.0.1";
$username = "root";
$password = "fullstack";
$database = "mysql";
return new \micro\db\Connection($servername, $username, $password, $database);
}
}
```Then you can instantiate the class.
#### Example
```php
// Create a new book
$book = new Book();
$book->title('This is the title of my book');
$book->save();// fetchs all books
$books = Book::find()->all();
foreach($books as $book) {
echo $book->title;
}// search for one book
$condition = [
['=', 'id', '1']
];
$book = Book::find()->where($condition)->one();
echo $book->title
```### The QueryBuilder Class
The queryBuilder class builds a Sql statement.
#### Example
```php
$table = 'home';
$qB = new \micro\db\QueryBuilder();
$columns = [
'id',
'name',
'address'
];
$sql = $qB->select($columns)->from($table)->getRawSql();
```The variable `$sql` is equal to the string "SELECT `id`, `name`, `address` FROM `home`".
### Contribution
Feel free to contribute! Just create a new issue or a new pull request.
### License
This library is released under the MIT License.