11# MicroOrm for PHP
22
3+ [ ![ Sponsor] ( https://img.shields.io/badge/Sponsor-%23ea4aaa?logo=githubsponsors&logoColor=white&labelColor=0d1117 )] ( https://github.com/sponsors/byjg )
34[ ![ Build Status] ( https://github.com/byjg/php-micro-orm/actions/workflows/phpunit.yml/badge.svg?branch=master )] ( https://github.com/byjg/php-micro-orm/actions/workflows/phpunit.yml )
45[ ![ Opensource ByJG] ( https://img.shields.io/badge/opensource-byjg-success.svg )] ( http://opensource.byjg.com )
56[ ![ GitHub source] ( https://img.shields.io/badge/Github-source-informational?logo=github )] ( https://github.com/byjg/php-micro-orm/ )
@@ -14,10 +15,21 @@ Key Features:
1415* Can be used with any DTO, Entity, Model or whatever class with public properties or with getter and setter
1516* The repository support a variety of datasources: MySql, Sqlite, Postgres, MySQL, Oracle (see byjg/anydataset)
1617* A class Mapper is used for mapping the Entity and the repository
18+ * Powerful mapper functions for automatic data transformation between models and database
1719* Small and simple to use
1820
1921## Architecture
2022
23+ MicroORM implements ** Martin Fowler's enterprise patterns** :
24+
25+ - ** [ Repository] ( https://martinfowler.com/eaaCatalog/repository.html ) ** : Mediates between domain and data mapping layers
26+ - ** [ Data Mapper] ( https://martinfowler.com/eaaCatalog/dataMapper.html ) ** : Separates domain objects from database tables
27+ - ** [ Active Record] ( https://martinfowler.com/eaaCatalog/activeRecord.html ) ** : Wraps database rows with domain logic (
28+ alternative approach)
29+
30+ You can choose the pattern that best fits your application: use Repository + Data Mapper for complex domains, or Active
31+ Record for simpler CRUD-focused applications.
32+
2133These are the key components:
2234
2335``` text
@@ -35,26 +47,35 @@ These are the key components:
3547│ ┌───────────────┴─────┐
3648│ │ Query │
3749│ └───────────────┬─────┘
38- │ │
39- │ ┌───────────────┴─────┐
40- │ │ DbDriverInterface │───────────────┐
41- │ └───────────────┬─────┘ │
42- │ │ │
43- └──────────────────────────┘ .─────────.
44- │ │
45- │`─────────'│
46- │ │
47- │ DB │
48- │ │
49- │ │
50- `─────────'
50+ │ │ │
51+ │ ┌───────────────┴─────┐ ┌──────────────────────┐
52+ │ │ DatabaseExecutor │────────│ DbDriverInterface │
53+ │ └───────────────┬─────┘ └────────────┬─────────┘
54+ │ │ │
55+ └──────────────────────────┘ .─────────.
56+ │ │
57+ │`─────────'│
58+ │ │
59+ │ DB │
60+ │ │
61+ │ │
62+ `─────────'
5163```
5264
53- * Model is a get/set class to retrieve or save the data into the database
54- * Mapper will create the definitions to map the Model into the Database.
55- * Query will use the Mapper to prepare the query to the database based on DbDriverInterface
56- * DbDriverIntarce is the implementation to the Database connection.
57- * Repository put all this together
65+ * ** Model** can be any class with public properties or with getter and setter. It is used to retrieve or save the data
66+ into the database
67+ * ** Mapper** defines the relationship between the Model properties and the database fields
68+ * ** FieldMapping** defines individual field mappings within the Mapper (field names, transformations, relationships via
69+ ` parentTable ` )
70+ * ** Query** defines what to retrieve from/update in the database. It uses the Mapper to prepare the query to the
71+ database converting the Model properties to database fields
72+ * ** DatabaseExecutor** (external package) wraps the DbDriver and provides transaction management, query execution, and
73+ access to database helpers
74+ * ** DbDriverInterface** (external package) is the actual database driver implementation that connects to the database
75+ * ** Repository** orchestrates all MicroORM components and uses DatabaseExecutor to interact with the database
76+
77+ For a detailed explanation of the architecture and when to use each layer,
78+ see [ Architecture Layers: Infrastructure vs Domain] ( docs/architecture-layers.md ) .
5879
5980
6081## Getting Started
@@ -89,20 +110,21 @@ Let's look at an example:
89110class MyModel
90111{
91112 #[FieldAttribute(primaryKey: true)]
92- public ?int $id;
113+ public ?int $id = null ;
93114
94115 #[FieldAttribute()]
95- public ?string $name;
116+ public ?string $name = null ;
96117
97118 #[FieldAttribute(fieldName: 'company_id')
98- public ?int $companyId;
119+ public ?int $companyId = null ;
99120}
100121```
101122
102123In this example, we have a class ` MyModel ` with three properties: ` id ` , ` name ` , and ` companyId ` .
103124
104- The ` id ` property is marked as a primary key. The ` name ` property is a simple field.
105- The ` companyId ` property is a field with a different name in the database ` company_id ` .
125+ * The ` id ` property is marked as a primary key.
126+ * The ` name ` property is a simple field.
127+ * The ` companyId ` property is a field with a different name in the database ` company_id ` .
106128
107129The ` TableAttribute ` is used to define the table name in the database.
108130
@@ -111,8 +133,7 @@ The `TableAttribute` is used to define the table name in the database.
111133After defining the Model, you can connect the Model with the repository.
112134
113135``` php
114- $dbDriver = \ByJG\AnyDataset\Db\Factory::getDbRelationalInstance('mysql://user:password@server/schema');
115-
136+ $dbDriver = \ByJG\AnyDataset\Db\Factory::getDbInstance('mysql://user:password@server/schema');
116137$repository = new \ByJG\MicroOrm\Repository($dbDriver, MyModel::class);
117138```
118139
@@ -153,6 +174,11 @@ $result = $repository->getByQuery($query);
153174* [ Querying the Database] ( docs/querying-the-database.md )
154175* [ Updating the database] ( docs/updating-the-database.md )
155176* [ Using the Mapper Object] ( docs/using-mapper-object.md )
177+ * [ The Model Attributes] ( docs/model-attribute.md )
178+ * [ The Repository Class] ( docs/repository.md )
179+ * [ Common Traits for Timestamp Fields] ( docs/common-traits.md )
180+ * [ Architecture Layers: Infrastructure vs Domain] ( docs/architecture-layers.md )
181+ * [ Comparison with Other ORMs (Eloquent, Doctrine)] ( docs/comparison-with-other-orms.md )
156182
157183## Advanced Topics
158184
@@ -163,10 +189,14 @@ $result = $repository->getByQuery($query);
163189* [ Caching the Results] ( docs/cache.md )
164190* [ Observing the Database] ( docs/observers.md )
165191* [ Controlling the data queried/updated] ( docs/controlling-the-data.md )
192+ * [ Mapper Functions] ( docs/mapper-functions.md )
166193* [ Using FieldAlias] ( docs/using-fieldalias.md )
167194* [ Tables without auto increments fields] ( docs/tables-without-auto-increment-fields.md )
168195* [ Using With Recursive SQL Command] ( docs/using-with-recursive-sql-command.md )
169196* [ QueryRaw (raw SQL)] ( docs/query-raw.md )
197+ * [ Update Constraints] ( docs/update-constraints.md )
198+ * [ Building SQL Queries] ( docs/query-build.md )
199+ * [ UUID Support] ( docs/uuid-support.md )
170200
171201## Install
172202
@@ -199,4 +229,4 @@ flowchart TD
199229```
200230
201231----
202- [ Open source ByJG] ( http://opensource.byjg.com )
232+ [ Open source ByJG] ( http://opensource.byjg.com )
0 commit comments