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
- Small and simple to use
These are the key components:
┌──────────────────────────┐
│ 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
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.
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;
#[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
.
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.
After defining the Model, you can connect the Model with the repository.
$dbDriver = \ByJG\AnyDataset\Db\Factory::getDbRelationalInstance('mysql://user:password@server/schema');
$repository = new \ByJG\MicroOrm\Repository($dbDriver, MyModel::class);
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);
- Active Record
- The Literal Object
- Soft Delete
- Caching the Results
- Observing the Database
- Controlling the data queried/updated
- Using FieldAlias
- Tables without auto increments fields
- Using With Recursive SQL Command
Just type:
composer require "byjg/micro-orm"
./vendor/bin/phpunit
flowchart TD
byjg/micro-orm --> byjg/anydataset-db
byjg/micro-orm --> ext-json