Componette

Componette

janpecha

janpecha / leanmapper-extension v1.0.0

Lean Mapper extension for Nette

download-cloud-line composer require janpecha/leanmapper-extension

LeanMapper-extension

Build Status Downloads this Month Latest Stable Version License

Lean Mapper extension for Nette.

Donate

Installation

Download a latest package or use Composer:

composer require janpecha/leanmapper-extension

Extension requires:

  • PHP 7.2 or later
  • Nette 3.0 or later
  • LeanMapper 4.0 or later

Usage

extensions:
	leanmapper: JP\LeanMapperExtension\LeanMapperExtension


leanmapper:
	# database connection
	username: ...
	password: ...
	database: ...

Configuration

Database connection

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

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

leanmapper:
	mapper: true # bool
	defaultEntityNamespace: 'Model\Entity'
	nameMapping: camelcase # default | camelcase | underscore
	prefix: null

Support for addons

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

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

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
Author: Jan Pecha, http://janpecha.iunas.cz/

  • v1.0.0 Version 1.0.0

    • required PHP 7.2 or newer
    • required Lean Mapper ^4.0
    • required Nette DI ^3.0 (BC BREAK)
    • fixed compatibility with nette/bootstrap 3.x

    Full Changelog: v0.13.3...v1.0.0

  • v0.13.3 Version 0.13.3

    • added support for alternative data structure returned from IRowMappingProvider::getRowFieldMappings()
    • added support for alternative data structure returned from IRowMappingProvider::getRowMultiValueMappings()
    • added support for alternative data structure returned from IStiMappingProvider::getStiMappings()

    New data structures are preferred.

    Full Changelog: v0.13.2...v0.13.3

  • v0.13.2 Version 0.13.2

    • fixed default value for connection charset to utf8mb4

    Full Changelog: v0.13.1...v0.13.2

  • v0.13.1 Version 0.13.1

    • added PHPStan - fixed PHPDoc annotations

    Full Changelog: v0.13.0...v0.13.1

  • v0.13.0 Version 0.13.0

    • added support for Lean Mapper 4.x
    • changed default value of option defaultEntityNamespace to Model\Entity (behavior of DefaultMapper changed in Lean Mapper 4.0)

    Full Changelog: v0.12.0...v0.13.0

  • v0.12.0 Version 0.12.0

    • added config option prefix for prefix of table names
    • added IStiMappingProvider
    • added IRowMappingProvider
    • changed default value of connection charsetto utf8mb (BC BREAK)
    • changed name of option mapping to entityMapping (BC BREAK)
    • changed type of option mapper to bool (BC BREAK)
    • added option nameMapping (default, camelcase, underscore, default value is camelcase)
    • removed JP\LeanMapperExtension\Mapper, used inlm/mappers:^2.1 instead (BC BREAK)
    • removed auto-registration of repositories (BC BREAK)

    Full Changelog: v0.11.0...v0.12.0

  • v0.11.0 Version 0.11.0

    • requires Lean Mapper ^3.0
    • requires Nette DI ^2.4
    • added posibility to disable registration of connection & entity factory
    • changed name of config option entities to mapping (BC BREAK)

    Full Changelog: v0.10.0...v0.11.0

  • v0.10.0 Version 0.10.0

    • added posibility to disable mapper registration
    • added posibility to configure charset for connection
    • dropped support for PHP 5.4 & 5.5

    Full Changelog: v0.9.2...v0.10.0

  • v0.9.2 Version 0.9.2

    • Composer: added tharos/leanmapper 3.x
    • Composer: updated constraints
    • updated coding-style
    • Travis CI: added PHP 7.1
    • Travis CI: updated
    • Tests: added Makefile
  • v0.9.1 Version 0.9.1

    • minor fixes, typos
  • v0.9.0 Version 0.9.0

    First public release

price-tag-2-line

Badges

guide-fill

Dependencies

php (>=5.3.0)
nette/di (^2.2)
tharos/leanmapper (^2.2 || ^3.0)
Componette Componette felix@nette.org