项目作者: marcomilon

项目描述 :
Lightweight ORM library.
高级语言: PHP
项目地址: git://github.com/marcomilon/micro-db.git
创建时间: 2017-11-23T18:35:38Z
项目社区:https://github.com/marcomilon/micro-db

开源协议:MIT License

下载


Micro-db

Latest Stable Version Build Status

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.
Then run

composer require fullstackpe/micro-db

If you prefer you can create a composer.json in your project folder.

  1. {
  2. "require": {
  3. "fullstackpe/micro-db": "^1.1"
  4. }
  5. }

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

  1. use micro\db\ActiveRecord;
  2. class Book extends ActiveRecord {
  3. public static function tableName()
  4. {
  5. return 'book';
  6. }
  7. public static function dbConnection()
  8. {
  9. $servername = "127.0.0.1";
  10. $username = "root";
  11. $password = "fullstack";
  12. $database = "mysql";
  13. return new \micro\db\Connection($servername, $username, $password, $database);
  14. }
  15. }

Then you can instantiate the class.

Example

  1. // Create a new book
  2. $book = new Book();
  3. $book->title('This is the title of my book');
  4. $book->save();
  5. // fetchs all books
  6. $books = Book::find()->all();
  7. foreach($books as $book) {
  8. echo $book->title;
  9. }
  10. // search for one book
  11. $condition = [
  12. ['=', 'id', '1']
  13. ];
  14. $book = Book::find()->where($condition)->one();
  15. echo $book->title

The QueryBuilder Class

The queryBuilder class builds a Sql statement.

Example

  1. $table = 'home';
  2. $qB = new \micro\db\QueryBuilder();
  3. $columns = [
  4. 'id',
  5. 'name',
  6. 'address'
  7. ];
  8. $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.