2017 © Pedro PelΓ‘ez
 

library micro-orm

A micro framework for create a very simple decoupled ORM (sqlite, mysql, postgres, sqlserver)

image

byjg/micro-orm

A micro framework for create a very simple decoupled ORM (sqlite, mysql, postgres, sqlserver)

  • Thursday, April 5, 2018
  • by byjg
  • Repository
  • 1 Watchers
  • 4 Stars
  • 9,376 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 13 Versions
  • 12 % Grown

The README.md

MicroOrm for PHP

Build Status Opensource ByJG GitHub source GitHub license GitHub release, (*1)

A micro framework for create a very simple decoupled ORM. This library intended to be very small and very simple to use;, (*2)

Key Features:, (*3)

  • Can be used with any DTO, Entity, Model or whatever class with public properties or with getter and setter
  • The repository support a variety of datasources: MySql, Sqlite, Postgres, MySQL, Oracle (see byjg/anydataset)
  • A class Mapper is used for mapping the Entity and the repository
  • Small and simple to use

Architecture

These are the key components:, (*4)

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Repository               β”‚              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                          β”‚         β”Œβ”€β”€β”€β”€β”‚        Model        β”‚
β”‚                          β”‚         β”‚    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”   β”‚               β”‚
β”‚          β”‚       Mapper        │────               β”‚
β”‚          β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜   β”‚               β”‚
β”‚                     β”‚    β”‚         β”‚    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                     β”‚    β”‚         └────│    FieldMapping     β”‚
β”‚                     β”‚    β”‚              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚                     β”‚    β”‚
β”‚          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”
β”‚          β”‚        Query        β”‚
β”‚          β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜
β”‚                          β”‚
β”‚          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”
β”‚          β”‚  DbDriverInterface  │───────────────┐
β”‚          β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜               β”‚
β”‚                          β”‚                     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                .─────────.
                                           β”‚           β”‚
                                           β”‚`─────────'β”‚
                                           β”‚           β”‚
                                           β”‚    DB     β”‚
                                           β”‚           β”‚
                                           β”‚           β”‚
                                            `─────────'
  • Model is a get/set class to retrieve or save the data into the database
  • Mapper will create the definitions to map the Model into the Database.
  • Query will use the Mapper to prepare the query to the database based on DbDriverInterface
  • DbDriverIntarce is the implementation to the Database connection.
  • Repository put all this together

Getting Started

Table Structure

We have the following table structure in the database for this example:, (*5)

CREATE TABLE `mytable` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `company_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

We want to be able to interact with this table using the ORM., (*6)

Defining the Model

A Model in our context is a class that symbolizes the data you wish to store or fetch from the database. This Model class can be as simple as a class with public properties. Alternatively, it can be a class equipped with getter and setter methods for more controlled access and manipulation of the data., (*7)

To map the database fields, you can add attributes to the Model class. Each property in the Model class represents a field in the database., (*8)

Let's look at an example:, (*9)

#[TableAttribute(tableName: 'mytable')]
class MyModel
{
    #[FieldAttribute(primaryKey: true)]
    public ?int $id;

    #[FieldAttribute()]
    public ?string $name;

    #[FieldAttribute(fieldName: 'company_id')
    public ?int $companyId;
}

In this example, we have a class MyModel with three properties: id, name, and companyId., (*10)

The id property is marked as a primary key. The name property is a simple field. The companyId property is a field with a different name in the database company_id., (*11)

The TableAttribute is used to define the table name in the database., (*12)

Connecting the repository

After defining the Model, you can connect the Model with the repository., (*13)

$dbDriver = \ByJG\AnyDataset\Db\Factory::getDbRelationalInstance('mysql://user:password@server/schema');

$repository = new \ByJG\MicroOrm\Repository($dbDriver, MyModel::class);

Querying the database

You can query the database using the repository., (*14)

$myModel = $repository->get(1);

or, (*15)

$query = Query::getInstance()
    ->field('name')
    ->where('company_id = :cid', ['cid' => 1]);

$result = $repository->getByQuery($query);

or, the same example above:, (*16)

$filterModel = $repository->entity([
    'company_id' => 1
]);

$query = $repository->queryInstance($filterModel);
$query->field('name');

$result = $repository->getByQuery($query);

Basics

Advanced Topics

Install

Just type:, (*17)

composer require "byjg/micro-orm"

Running Tests

./vendor/bin/phpunit

Dependencies

flowchart TD
    byjg/micro-orm --> byjg/anydataset-db
    byjg/micro-orm --> ext-json

Open source ByJG, (*18)

The Versions

05/04 2018

dev-master

9999999-dev

A micro framework for create a very simple decoupled ORM (sqlite, mysql, postgres, sqlserver)

  Sources   Download

MIT

The Requires

 

The Development Requires

by JoΓ£o Gilberto MagalhΓ£es

05/04 2018

2.2.2

2.2.2.0

A micro framework for create a very simple decoupled ORM (sqlite, mysql, postgres, sqlserver)

  Sources   Download

MIT

The Requires

 

The Development Requires

by JoΓ£o Gilberto MagalhΓ£es

11/03 2018

2.2.1

2.2.1.0

A micro framework for create a very simple decoupled ORM (sqlite, mysql, postgres, sqlserver)

  Sources   Download

MIT

The Requires

 

The Development Requires

by JoΓ£o Gilberto MagalhΓ£es

30/01 2018

2.2.0

2.2.0.0

A micro framework for create a very simple decoupled ORM (sqlite, mysql, postgres, sqlserver)

  Sources   Download

MIT

The Requires

 

The Development Requires

by JoΓ£o Gilberto MagalhΓ£es

26/01 2018

2.2.0.x-dev

2.2.0.9999999-dev

A micro framework for create a very simple decoupled ORM (sqlite, mysql, postgres, sqlserver)

  Sources   Download

MIT

The Requires

 

The Development Requires

by JoΓ£o Gilberto MagalhΓ£es

11/12 2017

2.1.1

2.1.1.0

A micro framework for create a very simple decoupled ORM (sqlite, mysql, postgres, sqlserver)

  Sources   Download

MIT

The Requires

 

by JoΓ£o Gilberto MagalhΓ£es

05/12 2017

2.1.0

2.1.0.0

A micro framework for create a very simple decoupled ORM (sqlite, mysql, postgres, sqlserver)

  Sources   Download

MIT

The Requires

 

by JoΓ£o Gilberto MagalhΓ£es

21/11 2017

2.0.1

2.0.1.0

A micro framework for create a very simple decoupled ORM (sqlite, mysql, postgres, sqlserver)

  Sources   Download

MIT

The Requires

 

by JoΓ£o Gilberto MagalhΓ£es

25/10 2017

2.0.1.x-dev

2.0.1.9999999-dev

A micro framework for create a very simple decoupled ORM (sqlite, mysql, postgres, sqlserver)

  Sources   Download

MIT

The Requires

 

by JoΓ£o Gilberto MagalhΓ£es

27/05 2017

2.0.0

2.0.0.0

A micro framework for create a very simple decoupled ORM (sqlite, mysql, postgres, sqlserver)

  Sources   Download

MIT

The Requires

 

by JoΓ£o Gilberto MagalhΓ£es

20/04 2017

1.0.1

1.0.1.0

A micro framework for create a very simple decoupled ORM

  Sources   Download

MIT

The Requires

 

by JoΓ£o Gilberto MagalhΓ£es

31/03 2017

1.0.1.x-dev

1.0.1.9999999-dev

A micro framework for create a very simple decoupled ORM

  Sources   Download

MIT

The Requires

 

by JoΓ£o Gilberto MagalhΓ£es

22/06 2016

1.0.0

1.0.0.0

A micro framework for create a very simple decoupled ORM

  Sources   Download

MIT

The Requires

 

by JoΓ£o Gilberto MagalhΓ£es