hgraca / micro-orm
A very small ORM library
Requires
- php: >=7.0
- hgraca/helper: ^1.2.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.0
- humbug/humbug: ^1.0@alpha
- mockery/mockery: ^0.9
- phpunit/php-code-coverage: ~3
- phpunit/phpunit: ~5
- roave/security-advisories: dev-master
This package is auto-updated.
Last update: 2023-07-30 19:16:49 UTC
README
A very small ORM library. It doesnt have any kind of caching, nor instance management. I've built it as a learning tool and maybe at some point it will be usable, but always as a very thin layer.
Usage
Config
The config can be something like:
[ 'repositoryFqcn' => MySqlPdoRepository::class, 'dateTimeFormat' => 'Y-m-d H:i:s', 'collectionFqcn' => Collection::class 'dataMapper' => [ Entity::class => [ 'entityFcqn' => Entity::class, // if not set, it will be inferred from the entity name, // if it doesnt exit, the default Repository will be used 'repositoryFqcn' => EntityRepository::class, 'table' => ClassHelper::extractCanonicalClassName(Entity::class), 'propertyToColumnNameMapper' => array_combine($properties, $properties), 'collectionFqcn' => Collection::class, 'attributes' => [ 'id' => [ // by convention its always 'id' 'column' => 'id', 'type' => 'integer', // by convention its always an integer ], 'aProperty' => [ 'column' => 'aColumn_name', 'type' => 'integer', // integer, float, boolean, string, text, datetime ], ], ], ], ]
Querying
For simple queries we can use the client select
method as:
$table = 'DummyTable'; $filter = [ 'propA' => true, 'propB' => null, 'propC' => ['filterC1' => 5, 'filterC2' => null], ]; $orderBy = [ 'propA' => 'ASC', 'propB' => 'DESC', ]; $this->client->select($table, $filter, $orderBy);
And the code above generates the following SQL:
SELECT * FROM `DummyTable` WHERE `propA`=:propA_filter AND `propB` IS :propB_filter AND (`propC`=:filterC1_filter OR `propC` IS :filterC2_filter) ORDER BY propA ASC, propB DESC
But for more complex queries we should do them custom and just pass them to the client executeQuery
method, ie:
$sql = 'SELECT * FROM `DummyTable` WHERE `propA`=:propA_filter AND `propB` IS :propB_filter AND (`propC`=:filterC1_filter OR `propC` IS :filterC2_filter) ORDER BY propA ASC, propB DESC'; $filter = [ 'propA' => true, 'propB' => null, 'propC' => ['filterC1' => 5, 'filterC2' => null], ]; $this->executeQuery($sql, $filter);
Conventions
- All entities have an ID, who's property name is 'id', column name is 'id' and type is int
- The default DB to be used is the first registered
Installation
To install the library, run the command below and you will get the latest version:
composer require hgraca/micro-orm
Tests
To run the tests run:
make test
Or just one of the following:
make test-acceptance make test-functional make test-integration make test-unit make test-humbug
To run the tests in debug mode run:
make test-debug
Coverage
To generate the test coverage run:
make coverage
Code standards
To fix the code standards run:
make cs-fix
Todo
- Cleanup and test Repository
- Cleanup and test DataMapper
- Cleanup and test Config
- Document how to use repositories and query classes, and how not to
- Create a relational config format, like the Doctrine yml config, but with arrays
- Implement lazy loading
- Create an EntityManager, management so we only save entities in the end of the request and works as a 1st level cache
- Implement 2nd level caching
- Implement eager loading