sagrishin/

lightweight-php-orm

轻量级ORM,用于数据库数据处理

v1.3.3 2018-03-08 22:38 UTC

This package is not auto-updated.

Last update: 2024-09-25 16:15:54 UTC


README

基于ActiveRecord模式的简单ORM。

使用方法:首先,你需要初始化数据库。创建一个以你的数据库名称命名的类(如果数据库名称和类名的大小写不匹配,可以将DB-name作为类的字段输入)

// 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

}

接下来,描述你的表

// 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();
    }

}

你还可以使用实体关系

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();
    }

}

现在你可以使用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();

更新 v1.3

现在可以从类中创建数据库表。你只需要描述类并调用迁移方法

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

请注意,当你对已存在于数据库中的表调用迁移时,它们的结构将被覆盖,并且所有数据将被删除。