https://github.com/janpecha/leanmapper-extension
Lean Mapper extension for Nette
https://github.com/janpecha/leanmapper-extension
leanmapper leanmapper-extension nette php
Last synced: 8 months ago
JSON representation
Lean Mapper extension for Nette
- Host: GitHub
- URL: https://github.com/janpecha/leanmapper-extension
- Owner: janpecha
- License: other
- Created: 2014-08-30T10:49:39.000Z (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2024-06-27T06:43:03.000Z (about 2 years ago)
- Last Synced: 2025-01-16T00:22:09.866Z (over 1 year ago)
- Topics: leanmapper, leanmapper-extension, nette, php
- Language: PHP
- Homepage:
- Size: 79.1 KB
- Stars: 4
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
- Funding: .github/funding.yml
- License: license.md
Awesome Lists containing this project
README
LeanMapper-extension
====================
[](https://github.com/janpecha/leanmapper-extension/actions)
[](https://packagist.org/packages/janpecha/leanmapper-extension)
[](https://github.com/janpecha/leanmapper-extension/releases)
[](https://github.com/janpecha/leanmapper-extension/blob/master/license.md)
[Lean Mapper](http://leanmapper.com/) extension for [Nette](https://nette.org).
Installation
------------
[Download a latest package](https://github.com/janpecha/leanmapper-extension/releases) or use [Composer](http://getcomposer.org/):
```
composer require janpecha/leanmapper-extension
```
Extension requires:
* PHP 7.2 or later
* Nette 3.0 or later
* LeanMapper 4.0 or later
Usage
-----
``` neon
extensions:
leanmapper: JP\LeanMapperExtension\LeanMapperExtension
leanmapper:
# database connection
username: ...
password: ...
database: ...
```
Configuration
-------------
### Database connection
``` neon
leanmapper:
# required
username: ...
password: ...
database: ...
# optional
connection: LeanMapper\Connection
host: localhost
driver: mysqli
lazy: true
profiler: ... # on|off or NULL => enabled in debug mode, disabled in production mode
charset: utf8mb
```
### Entities
``` neon
leanmapper:
entityFactory: LeanMapper\DefaultEntityFactory
entityMapping:
table: EntityClass
table:
entity: EntityClass
repository: RepositoryClass # only mapping, you need manually register repository to DI
primaryKey: table_primary_key
articles:
entity: App\Model\Article
primaryKey: article_id
```
### Mapper
``` neon
leanmapper:
mapper: true # bool
defaultEntityNamespace: 'Model\Entity'
nameMapping: camelcase # default | camelcase | underscore
prefix: null
```
Support for addons
------------------
``` php
use Nette\DI\CompilerExtension;
use JP\LeanMapperExtension\IEntityProvider;
class FooExtension extends CompilerExtension implements IEntityProvider
{
// from IEntityProvider
function getEntityMappings()
{
return array(
array(
'table' => 'foo_articles',
'primaryKey' => 'id',
'entity' => Foo\Model\Article::class,
'repository' => Foo\Model\ArticleRepository::class, # only mapping, you need manually register repository to DI
),
// ...
);
}
}
```
STI mapping
``` php
use Nette\DI\CompilerExtension;
use JP\LeanMapperExtension\IStiMappingProvider;
class FooExtension extends CompilerExtension implements IStiMappingProvider
{
function getStiMappings()
{
return [
Model\Entity\Client::class => [ // base entity
// type => target entity
'company' => Model\Entity\ClientCompany::class,
],
// ...
];
}
function getStiTypeFields()
{
return [
Model\Entity\Client::class => 'clientType',
];
}
}
```
Row mapping
``` php
use Nette\DI\CompilerExtension;
use JP\LeanMapperExtension\IRowMappingProvider;
class FooExtension extends CompilerExtension implements IRowMappingProvider
{
function getRowFieldMappings()
{
return [
\Model\Entity\OrderItem::class => [
'currency' => [
'fromDbValue' => [static::class, 'currencyFromDb'],
'toDbValue' => [static::class, 'currencyToDb'],
]
],
// ...
];
}
function getRowMultiValueMappings()
{
return [
\Model\Entity\OrderItem::class => [
'price' => [
'fromDbValue' => [static::class, 'priceFromDb'],
'toDbValue' => [static::class, 'priceToDb'],
],
],
];
}
static function currencyFromDb($value)
{
return strtoupper($value);
}
static function currencyToDb($value)
{
return strtolower($value);
}
static function priceFromDb(array $values)
{
return [$values['price'], $values['currency']];
}
static function priceToDb($value)
{
return [
'price' => $value[0],
];
}
}
```
------------------------------
License: [New BSD License](license.md)
Author: Jan Pecha, http://janpecha.iunas.cz/