Micro-db
, (*1)
Micro-db is a lightweight ORM library., (*2)
Installation
First you need to install Composer. You may do so by following the instructions
at getcomposer.org.
Then run, (*3)
composer require fullstackpe/micro-db, (*4)
If you prefer you can create a composer.json in your project folder., (*5)
{
"require": {
"fullstackpe/micro-db": "^1.1"
}
}
Then run the command, (*6)
composer install, (*7)
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()., (*8)
Example
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., (*9)
Example
// 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., (*10)
Example
$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
"., (*11)
Contribution
Feel free to contribute! Just create a new issue or a new pull request., (*12)
License
This library is released under the MIT License., (*13)