Skip to main content

MicroOrm for PHP

Sponsor Build Status Opensource ByJG GitHub source GitHub license GitHub release

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

Key Features:

  • 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
  • Powerful mapper functions for automatic data transformation between models and database
  • Small and simple to use

Architecture

MicroORM implements Martin Fowler's enterprise patterns:

  • Repository: Mediates between domain and data mapping layers
  • Data Mapper: Separates domain objects from database tables
  • Active Record: Wraps database rows with domain logic ( alternative approach)

You can choose the pattern that best fits your application: use Repository + Data Mapper for complex domains, or Active Record for simpler CRUD-focused applications.

These are the key components:

┌──────────────────────────┐
│ Repository │ ┌─────────────────────┐
│ │ ┌────│ Model │
│ │ │ └─────────────────────┘
│ ┌───────────────┴─────┐ │ │
│ │ Mapper │───┤ │
│ └───────────────┬─────┘ │ │
│ │ │ │ ┌─────────────────────┐
│ │ │ └────│ FieldMapping │
│ │ │ └─────────────────────┘
│ │ │
│ ┌───────────────┴─────┐
│ │ Query │
│ └───────────────┬─────┘
│ │ │
│ ┌───────────────┴─────┐ ┌──────────────────────┐
│ │ DatabaseExecutor │────────│ DbDriverInterface │
│ └───────────────┬─────┘ └────────────┬─────────┘
│ │ │
└──────────────────────────┘ .─────────.
│ │
│`─────────'│
│ │
│ DB │
│ │
│ │
`─────────'
  • Model can be any class with public properties or with getter and setter. It is used to retrieve or save the data into the database
  • Mapper defines the relationship between the Model properties and the database fields
  • FieldMapping defines individual field mappings within the Mapper (field names, transformations, relationships via parentTable)
  • Query defines what to retrieve from/update in the database. It uses the Mapper to prepare the query to the database converting the Model properties to database fields
  • DatabaseExecutor (external package) wraps the DbDriver and provides transaction management, query execution, and access to database helpers
  • DbDriverInterface (external package) is the actual database driver implementation that connects to the database
  • Repository orchestrates all MicroORM components and uses DatabaseExecutor to interact with the database

For a detailed explanation of the architecture and when to use each layer, see Architecture Layers: Infrastructure vs Domain.

Getting Started

Table Structure

We have the following table structure in the database for this example:

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.

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.

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.

Let's look at an example:

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

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

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

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

  • 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.

The TableAttribute is used to define the table name in the database.

Connecting the repository

After defining the Model, you can connect the Model with the repository.

$dbDriver = \ByJG\AnyDataset\Db\Factory::getDbInstance('mysql://user:password@server/schema');
$repository = new \ByJG\MicroOrm\Repository($dbDriver, MyModel::class);

Querying the database

You can query the database using the repository.

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

or

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

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

or, the same example above:

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

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

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

Basics

Advanced Topics

Install

Just type:

composer require "byjg/micro-orm"

Running Tests

./vendor/bin/phpunit

Dependencies


Open source ByJG