2017 © Pedro Peláez
 

library simpleorm

SimpleORM

image

fforattini/simpleorm

SimpleORM

  • Tuesday, November 22, 2016
  • by filipeforattini
  • Repository
  • 2 Watchers
  • 0 Stars
  • 9 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

SimpleORM (Under development)

Software License, (*1)

SimpleORM is an intuitive package to work with Entity and Repository abstractions., (*2)

This package was developed using Doctrine\DBAL package to ensure quality and versatility of development., (*3)

Get started

Installing

Install using Composer:, (*4)

composer require "fforattini/simpleorm"

Read the unit tests files for faster understanding of the package: + Entity + Repository, (*5)

How to use

SimpleORM has two abstractions Entity and Repository., (*6)

Entity

The Entity should represent an table on your database., (*7)

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:, (*8)

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

Example:, (*9)

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:, (*10)

$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., (*11)

Mocking data

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

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:, (*13)

$book = Book::factory();

Or you can just pass a callable as parameter and you will receive an instance of the Generator:, (*14)

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) :, (*15)

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:, (*16)

$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:, (*17)

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

Or you can just pass a callable as parameter and you will receive an instance of the Generator:, (*18)

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)., (*19)

This is where all of SQL queries are trigged using the Doctrine\DBAL\Connection., (*20)

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., (*21)

$books->all();

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

The Versions

22/11 2016

dev-master

9999999-dev

SimpleORM

  Sources   Download

MIT

The Requires

 

The Development Requires

by Filipe Forattini