PHPackages                             fforattini/simpleorm - 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. fforattini/simpleorm

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

fforattini/simpleorm
====================

SimpleORM

016[1 PRs](https://github.com/filipeforattini/SimpleORM/pulls)

Since Nov 22Compare

[ Source](https://github.com/filipeforattini/SimpleORM)[ Packagist](https://packagist.org/packages/fforattini/simpleorm)[ RSS](/packages/fforattini-simpleorm/feed)WikiDiscussions Synced 6d ago

READMEChangelogDependenciesVersions (2)Used By (0)

SimpleORM (Under development)
=============================

[](#simpleorm-under-development)

[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)

SimpleORM is an intuitive package to work with `Entity` and `Repository` abstractions.

This package was developed using [`Doctrine\DBAL`](docs.doctrine-project.org/projects/doctrine-dbal/en/latest/) package to ensure quality and versatility of development.

Get started
-----------

[](#get-started)

### Installing

[](#installing)

Install using Composer:

```
composer require "fforattini/simpleorm"
```

Read the unit tests files for faster understanding of the package:

- [Entity](tests/EntityTests.php)
- [Repository](tests/RepositoryTests.php)

How to use
----------

[](#how-to-use)

SimpleORM has two abstractions `Entity` and `Repository`.

### Entity

[](#entity)

The `Entity` should represent an table on your database.

```
use SimpleORM\Entity;

class Book extends Entity
{

}
```

#### Custom properties

[](#custom-properties)

Most of the `Entity` attributes are defined by `SimpleORM`, but of course you can define your own values for them:

- `protected static $_table` will be define as the plural of the class name using the package `icanboogie/inflector`;

Example:

```
use SimpleORM\Entity;

class Book extends Entity
{
    public static $_table = 'my_books_table';
}
```

#### Attributes

[](#attributes)

The class `Entity` extends an `ArrayObject`! So there is an list of possibilities for you to work with your attributes:

```
$book = new Book();
$book->id = '02adee84-a128-4e51-8170-4155ea222fae';
$book->name = 'My book';

// OR

$book = new Book([
    'id' => '02adee84-a128-4e51-8170-4155ea222fae',
    'name' => 'My book',
]);
```

Learn more about [`ArrayObject` here with the docs](http://php.net/manual/en/class.arrayobject.php).

#### Mocking data

[](#mocking-data)

Simply define a factory of elements using `fzaninotto/faker` (learn more about this [with the docs](https://github.com/fzaninotto/Faker)):

```
use Faker\Generator;
use Ramsey\Uuid\Uuid;
use SimpleORM\Entity;

class Book extends Entity
{
    public static function defineFactory(Generator $faker)
    {
        return [
            'id' => Uuid::uuid4(),
            'name' => $faker->sentence(5, true),
        ];
    }
}
```

Then you can just call:

```
$book = Book::factory();
```

Or you can just pass a `callable` as parameter and you will receive an instance of the `Generator`:

```
use Ramsey\Uuid\Uuid;

$book = Book::factory(function($faker){
    return [
        'id' => Uuid::uuid4(),
        'name' => $faker->sentence(5, true),
    ];
});
```

#### Creating tables

[](#creating-tables)

You can define your table using a `Doctrine\DBAL\Schema\Table` instance through the function `Entity::defineTable(Table $table)` :

```
use SimpleORM\Entity;
use SimpleORM\TableCreator;
use Doctrine\DBAL\Schema\Table;

class Book extends Entity implements TableCreator
{
    public static function defineTable(Table $table)
    {
        $table->addColumn('id', 'string', [
            'length' => 36,
            'unique' => true,
        ]);
        $table->addColumn('name', 'string');
        $table->addUniqueIndex(["id"]);
        $table->setPrimaryKey(['id']);
        return $table;
    }
}
```

You will need to use the `Doctrine\DBAL\DriverManager` to get a `Connection` (learn more about this [with the docs](http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html)) and create your table:

```
$schema = DriverManager::getConnection([
    'driver'    => 'pdo_sqlite',
    'path'      => 'database.sqlite',
])->getSchemaManager();
```

Then you can create your table using the connection you create with the following method:

```
$schema->createTable(Book::getTable());
```

Or you can just pass a `callable` as parameter and you will receive an instance of the `Generator`:

```
use SimpleORM\Entity;

$schema->createTable(Entity::getTable(function($table){
    $table->addColumn('id', 'string', [
        'length' => 36,
        'unique' => true,
    ]);
    $table->addColumn('name', 'string');
    $table->addUniqueIndex(["id"]);
    $table->setPrimaryKey(['id']);
    return $table;
}));
```

### Repository

[](#repository)

The `Repository` should deal with a collection of objects of `Entity` and implements `SplDoublyLinkedList` (you can check [more about it here](http://php.net/manual/en/class.spldoublylinkedlist.php)).

This is where all of `SQL` queries are trigged using the `Doctrine\DBAL\Connection`.

```
use SimpleORM\Repository;
use Doctrine\DBAL\DriverManager;

DriverManager::getConnection([
    'driver' => 'pdo_sqlite',
    'path' => 'database.sqlite',
]);

$books = new Repository(Book::class, $connection);
```

#### Retriving information

[](#retriving-information)

To populate your `Repository` with all of your elements.

```
$books->all();

foreach($books as $book) {
    // do something here
}
```

###  Health Score

20

—

LowBetter than 12% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community2

Small or concentrated contributor base

Maturity43

Maturing project, gaining track record

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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/8e3e44d2d781008639c5fb3844db8d73a5901ee1f6afd0a2f2144449759d139f?d=identicon)[filipeforattini](/maintainers/filipeforattini)

### Embed Badge

![Health badge](/badges/fforattini-simpleorm/health.svg)

```
[![Health](https://phpackages.com/badges/fforattini-simpleorm/health.svg)](https://phpackages.com/packages/fforattini-simpleorm)
```

###  Alternatives

[jdorn/sql-formatter

a PHP SQL highlighting library

3.8k117.8M121](/packages/jdorn-sql-formatter)[backup-manager/backup-manager

A framework agnostic database backup manager with user-definable procedures and support for S3, Dropbox, FTP, SFTP, and more with drivers for popular frameworks.

1.7k1.6M11](/packages/backup-manager-backup-manager)[propel/propel1

Propel is an open-source Object-Relational Mapping (ORM) for PHP5.

8351.6M88](/packages/propel-propel1)[insolita/yii2-migration-generator

Set of gii tools for generating files for migration by schema of table , phpdoc or table data

108508.0k5](/packages/insolita-yii2-migration-generator)[xpdo/xpdo

A PDO-based Object/Relational Bridge Library

7088.4k4](/packages/xpdo-xpdo)[voku/session2db

A PHP library acting as a wrapper for PHP's default session handling functions which stores data in a MySQL database, providing both better performance and better security and protection against session fixation and session hijacking.

3220.0k](/packages/voku-session2db)

PHPackages © 2026

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