dev-master
9999999-devSimpleORM
MIT
The Requires
The Development Requires
by Filipe Forattini
Wallogit.com
2017 © Pedro Peláez
SimpleORM
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)
Install using Composer:, (*4)
composer require "fforattini/simpleorm"
Read the unit tests files for faster understanding of the package: + Entity + Repository, (*5)
SimpleORM has two abstractions Entity and Repository., (*6)
The Entity should represent an table on your database., (*7)
use SimpleORM\Entity;
class Book extends Entity
{
}
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';
}
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)
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),
];
});
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;
}));
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);
To populate your Repository with all of your elements., (*21)
$books->all();
foreach($books as $book) {
// do something here
}
SimpleORM
MIT