PHPackages                             walnut/lib\_modelmapper\_orm - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. walnut/lib\_modelmapper\_orm

ActiveLibrary[Database &amp; ORM](/categories/database)

walnut/lib\_modelmapper\_orm
============================

049PHP

Since May 13Pushed 4y ago1 watchersCompare

[ Source](https://github.com/kapitancho/walnut-lib-modelmapper-orm)[ Packagist](https://packagist.org/packages/walnut/lib_modelmapper_orm)[ RSS](/packages/walnut-lib-modelmapper-orm/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependenciesVersions (1)Used By (0)

ORM based Model Mapper
======================

[](#orm-based-model-mapper)

An adapter to use Relational Storage as a Model Mapper

Example
=======

[](#example)

This is a very simple data model - client.

```
class Client {
	public function __construct(
		public /*readonly*/ string $id,
		public /*readonly*/ string $name
	) {}
}
```

A serialization/deserialization logic is required.

```
/**
 * @implements ModelBuilder
 * @implements ModelParser
 */
final class ClientSerializer implements ModelBuilder, ModelParser {

	/**
	 * @param array $source
	 * @return Client
	 */
	public function build(array $source): object {
		return new Client(
			$source['id'] ?? '',
			$source['name'] ?? ''
		);
	}

	/**
	 * @param Client $source
	 * @return array
	 */
	public function parse(object $source): array {
		return [
			'id' => $source->id,
			'name' => $source->name
		];
	}
}
```

All serializers can be aggregated into a factory class

```
final class MapperFactory implements ModelBuilderFactory, ModelParserFactory {

	/**
	 * @param string $className
	 * @return ModelBuilder&ModelParser
	 */
	private function getSerializer(string $className): object /*ModelBuilder&ModelParser*/ {
		return match($className) {
			Client::class => new ClientSerializer,
			default => throw new RuntimeException("Unknown class $className")
		};
	}

	public function getBuilder(string $className): ModelBuilder {
		return $this->getSerializer($className);
	}

	public function getParser(string $className): ModelParser {
		return $this->getSerializer($className);
	}

}
```

### Defining the relational model

[](#defining-the-relational-model)

```
#[ModelRoot('clients')]
class ClientDbModel {
    public function __construct(
        #[Table("clients")]
        #[KeyField('id'), Fields('name')]
        public array $clients
    ) {}
}
```

Using a MySql storage

```
$sqlQuoter = new MysqlQuoter;
$queryExecutor = new PdoQueryExecutor($connector);
$dataModelFactory = new DataModelFactory($sqlQuoter, $queryExecutor);

$serializerFactory = new MapperFactory;

//Mapping between the models and their storage keys:
$configuration = new OrmModelMapperConfiguration(
    [Client::class => ClientDbModel::class]
);

//Mapper factory covering all models
$modelMapperFactory = new OrmModelMapperFactory(
    $configuration,
    new DataModelBuilder,
    $dataModelFactory,
    $serializerFactory,
    $serializerFactory,
    $sqlQuoter
);

//Take the mapper for the Client model
$mapper = $modelMapperFactory->getMapper(Client::class);

//Prepare two records
$firstClient = new Client('cl-1', 'Client 1');
$secondClient = new Client('cl-2', 'Client 2');

//Using the storage
$mapper->all(); //0 entries

$mapper->store($firstClient->id, $firstClient);
$mapper->store($secondClient->id, $secondClient);

$mapper->all(); //2 entries

$updatedSecondClient = new Client('cl-2', 'Client 2 new name');

$mapper->store($updatedSecondClient->id, $updatedSecondClient);
$mapper->all(); //2 entries

$mapper->byId($firstClient->id)->name; //Client 1
$mapper->byId($updatedSecondClient->id)->name; //Client 2 new name

$mapper->remove($firstClient->id);
$mapper->byId($firstClient->id); //null

$mapper->all(); //1 entry
```

###  Health Score

16

—

LowBetter than 5% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity26

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/12d2910601119ce65c373b7e21ffc647154bde10c5ad0a0598ab1143aa851701?d=identicon)[kapitancho](/maintainers/kapitancho)

---

Top Contributors

[![kapitancho](https://avatars.githubusercontent.com/u/792682?v=4)](https://github.com/kapitancho "kapitancho (7 commits)")

### Embed Badge

![Health badge](/badges/walnut-lib-modelmapper-orm/health.svg)

```
[![Health](https://phpackages.com/badges/walnut-lib-modelmapper-orm/health.svg)](https://phpackages.com/packages/walnut-lib-modelmapper-orm)
```

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[mongodb/mongodb

MongoDB driver library

1.6k64.0M546](/packages/mongodb-mongodb)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90340.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
