byjg / micro-orm
A micro framework for create a very simple decoupled ORM (sqlite, mysql, postgres, sqlserver)
Fund package maintenance!
byjg
Installs: 38 545
Dependents: 4
Suggesters: 0
Security: 0
Stars: 17
Watchers: 3
Forks: 4
Open Issues: 1
Requires
- php: >=8.1 <8.4
- ext-json: *
- byjg/anydataset-db: ^5.0
Requires (Dev)
- phpunit/phpunit: ^9.6
- vimeo/psalm: ^5.9
Suggests
- ext-curl: *
- ext-pdo_mysql: *
README
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
Architecture
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
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; #[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.
Connecting the repository
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);
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
- 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
Install
Just type:
composer require "byjg/micro-orm"
Running Tests
./vendor/bin/phpunit