fforattini/simpleorm

dev-master 2016-11-22 12:49 UTC

This package is not auto-updated.

Last update: 2024-04-27 17:53:21 UTC


README

Software License

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

This package was developed using Doctrine\DBAL package to ensure quality and versatility of development.

Get started

Installing

Install using Composer:

composer require "fforattini/simpleorm"

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

How to use

SimpleORM has two abstractions Entity and Repository.

Entity

The Entity should represent an table on your database.

use SimpleORM\Entity;

class Book extends Entity 
{

}

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

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.

Mocking data

Simply define a factory of elements using fzaninotto/faker (learn more about this with the docs):

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

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

The Repository should deal with a collection of objects of Entity and implements SplDoublyLinkedList (you can check more about it here).

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

To populate your Repository with all of your elements.

$books->all();

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