Hgraca\MicroOrm
, (*1)
, (*2)
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., (*3)
Usage
Config
The config can be something like:, (*4)
[
'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:, (*5)
$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:, (*6)
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:, (*7)
$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:, (*8)
composer require hgraca/micro-orm
Tests
To run the tests run:, (*9)
make test
Or just one of the following:, (*10)
make test-acceptance
make test-functional
make test-integration
make test-unit
make test-humbug
To run the tests in debug mode run:, (*11)
make test-debug
Coverage
To generate the test coverage run:, (*12)
make coverage
Code standards
To fix the code standards run:, (*13)
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