定制 sagrishin/lightweight-php-orm 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

sagrishin/lightweight-php-orm

Composer 安装命令:

composer require sagrishin/lightweight-php-orm

包简介

Lightweight ORM for manipulating data in database

README 文档

README

Simple ORM based on pattern ActiveRecord.

How to use: Firstly you need to init DataBase. Create class with name of your database (If the names of the database and the class do not match the case of letters, you can enter DB-name as field of class)

// you need to extend AbstractDataBase class (HomeLibrary.php)
final class HomeLibrary extends AbstractDataBase
{

    public $dbtype = "mysql";           // driver for connection and executing queries
    public $dbname = "homelibrary";     // database name (dbname and this class are not equal by case of letters)
    public $user = "root";              // login
    public $password = "1111";          // password

}

Next, describe your tables:

// you need to extend Table class -- base table class (Book.php)
class Book extends Table
{
    // fields of table in database
    public $id;
    public $book;

    public function __construct()
    {
        $this->table_name = "books";       // if name of table and this class are not equal, place name of table in this field
        // describe fields type
        $this->id = Field::primaryKey();   // describe PrimaryKey (with auto_increment)
        $this->book = Field::varchar(100); // describe varchar field of 100 symbools 
        $this->initTable();                // call method for initialisation table
    }

}

// the same for another table(s) (Author.php)
class Author extends Table
{

    public $id;
    public $author;

    public function __construct()
    {
        $this->table_name = "authors";

        $this->id = Field::primaryKey();
        $this->author = Field::varchar(100);
        $this->initTable();
    }

}

Also you can use entity relationships:

class Library extends Table
{

    public $id;
    public $book;
    public $author;

    public function __construct()
    {
        $this->table_name = "library";

        $this->id = Field::primaryKey();
        // describe foreign key with cascade delete and update
        // You need to place class and field in it for foreign key
        $this->book = Field::foreignKey(Book::class, "id", [
            "on_delete" => "cascade", "on_update" => "cascade"
        ]);
        $this->author = Field::foreignKey(Author::class, "id", [
            "on_delete" => "cascade", "on_update" => "cascade"
        ]);
        $this->initTable();
    }

}

Now you can use ORM:

// (index.php)

// now init your DataBase
$db = new HomeLibrary();

// then you can use ORM:

/* add new book */
$book1 = new Book();
$book1->book = "Book_56";
$book1->save();
/* add new author */
$author1 = new Author();
$author1->author = "Author1";
$author1->save();
/* add information about author and book in library */
$library = new Library();
$library->book = $book1;
$library->author = $author1;
$library->save();

/* find all information about book with id 9 in library */
$lib = Library::find(["book" => 6])[0];
print_r("ID " . $lib->id . "\n");
print_r("Book ID " . $lib->book->id . "\n");
print_r("Book " . $lib->book->book . "\n");
print_r("Author ID " . $lib->author->id . "\n");
print_r("Author " . $lib->author->author . "\n");

/* list of all books and aouthors from library */
$lib = Library::listAll();

foreach ($lib as $item) {
    print_r("ID " . $item->id . "\n");
    print_r("Book ID " . $item->book->id . "\n");
    print_r("Book " . $item->book->book . "\n");
    print_r("Author ID " . $item->author->id . "\n");
    print_r("Author " . $item->author->author . "\n");
}

/* remove book with name Book_56 from database */
Book::findFirst(["book" => "Book_56"])->remove();

update v1.3:

Now it's possible to create tables in database from classes. All you need is describe classes and call migrate method:

// migrate.php
$db = new HomeLibrary();
$book1 = new Book();
$book1->migrate();
$author1 = new Author();
$author1->migrate();
$library = new Library();
$library->migrate();

Be careful, when you call migrate for tables which are exists in database, their structure will be overwritten and all data will deleted.

统计信息

  • 总下载量: 184
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 7
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-06-18

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固