PHPackages                             sagrishin/lightweight-php-orm - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. sagrishin/lightweight-php-orm

ActiveLibrary[Database &amp; ORM](/categories/database)

sagrishin/lightweight-php-orm
=============================

Lightweight ORM for manipulating data in database

v1.3.3(8y ago)71842[1 issues](https://github.com/GrishinSergey/lightweight-php-orm/issues)MITPHPPHP ^7.0

Since Jun 18Pushed 8y ago1 watchersCompare

[ Source](https://github.com/GrishinSergey/lightweight-php-orm)[ Packagist](https://packagist.org/packages/sagrishin/lightweight-php-orm)[ RSS](/packages/sagrishin-lightweight-php-orm/feed)WikiDiscussions master Synced 4d ago

READMEChangelog (8)DependenciesVersions (10)Used By (0)

lightweight-php-orm [![code style](https://camo.githubusercontent.com/0cb42677e070a72c8d3e80134899c654fd6e1122370c2a64f9a5cbcd9cdd88b4/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5374796c6543492d7061737365642d677265656e2e737667)](https://camo.githubusercontent.com/0cb42677e070a72c8d3e80134899c654fd6e1122370c2a64f9a5cbcd9cdd88b4/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5374796c6543492d7061737365642d677265656e2e737667)
===================================================================================================================================================================================================================================================================================================================================================================================================================================================================

[](#lightweight-php-orm-)

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:
============

[](#update-v13)

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.

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance17

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~33 days

Recently: every ~64 days

Total

9

Last Release

2990d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/557414252ae828c469d4cda4d7a90d3945dc03c432b0a7c08b96cc73f447ea66?d=identicon)[GrishinSergey](/maintainers/GrishinSergey)

---

Top Contributors

[![GrishinSergey](https://avatars.githubusercontent.com/u/12826416?v=4)](https://github.com/GrishinSergey "GrishinSergey (3 commits)")

---

Tags

databasemysql-ormormphpphp-ormsql

### Embed Badge

![Health badge](/badges/sagrishin-lightweight-php-orm/health.svg)

```
[![Health](https://phpackages.com/badges/sagrishin-lightweight-php-orm/health.svg)](https://phpackages.com/packages/sagrishin-lightweight-php-orm)
```

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[mongodb/mongodb

MongoDB driver library

1.6k64.0M546](/packages/mongodb-mongodb)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90340.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
