Download the PHP package byjg/micro-orm without Composer
On this page you can find all versions of the php package byjg/micro-orm. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download byjg/micro-orm
More information about byjg/micro-orm
Files in byjg/micro-orm
Package micro-orm
Short Description A micro framework for create a very simple decoupled ORM (sqlite, mysql, postgres, sqlserver)
License MIT
Informations about the package micro-orm
MicroOrm for PHP
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:
- 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
Examples
For the examples below we will use the class 'Users';
First of all will create a Table Mapping class:
Then you need to create the dataset object and the repository:
Some examples with the repository:
Update Constraints
You can define a constraint to update a record. If the constraint is not satisfied the update will not be performed.
Advanced uses
Get a collection using the query object:
Returning multiples entities with a query:
Using FieldAlias
Field alias is an alternate name for a field. This is usefull for disambiguation on join and leftjoin queries. Imagine in the example above if both tables ITEM and ORDER have the same field called 'ID'.
In that scenario, the value of ID will be overriden. The solution is use the FieldAlias like below:
You can also add a MAPPER as a Field. In that case the MAPPER will create the field and the correct aliases.
Tables without auto increments fields
Applying functions for Select and Update
Observers
You can add observers to the repository. The observer will be called after the insert, update or delete a record in the DB.
The ObserverData
class contains the following properties:
getTable()
: The table name that was affectedgetEvent()
: The event that was triggered. Can be 'insert', 'update' or 'delete'getData()
: The data that was inserted or updated. It is null in case of delete.getOldData()
: The data before update. In case of insert comes null, and in case of delete comes with the param filters.getRepository()
: The repository is listening to the event (the same as $myRepository)
Note: The observer will not be called if the insert, update or delete is called using the DBDriver object.
Using With Recursive SQL Command
Pre-defined closures for field map
Mapper::defaultClosure($value, $instance)
Defines the basic behavior for select and update fields;
Mapper::doNotUpdateClosure($value, $instance)
If set in the update field map will make the field not updatable by the micro-orm. It is usefull for fields that are pre-defined like 'Primary Key'; timestamp fields based on the update and the creation; and others
Before insert and update functions
You can also set closure to be applied before insert or update a record. In this case will set in the Repository:
TransactionManager object
It allows you to create a single database transaction with multiple repositories. If any of the repositories fails the transaction will be rolled back for all repositories. When you commit the transaction all repositories will be commited.
Install
Just type: