janpecha / leanmapper-extension
针对 Nette DI 的 LeanMapper 扩展
v1.0.0
2023-07-12 05:12 UTC
Requires
- php: >=7.2.0
- czproject/assert: ^1.3
- inlm/mappers: ^2.1 || ^3.0
- nette/di: ^3.0
- tharos/leanmapper: ^4.0
Requires (Dev)
- nette/bootstrap: ^3.0
- nette/tester: ^2.0
- tracy/tracy: ^2.6
README
Lean Mapper 扩展,适用于 Nette.
安装
下载最新包 或使用 Composer
composer require janpecha/leanmapper-extension
扩展要求
- PHP 7.2 或更高版本
- Nette 3.0 或更高版本
- LeanMapper 4.0 或更高版本
用法
extensions: leanmapper: JP\LeanMapperExtension\LeanMapperExtension leanmapper: # database connection username: ... password: ... database: ...
配置
数据库连接
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
实体
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
映射器
leanmapper: mapper: true # bool defaultEntityNamespace: 'Model\Entity' nameMapping: camelcase # default | camelcase | underscore prefix: null
对插件的支持
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 映射
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', ]; } }
行映射
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], ]; } }
许可证: 新 BSD 许可证
作者:Jan Pecha, http://janpecha.iunas.cz/