PHPackages                             earc/data-primary-key-generator - 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. earc/data-primary-key-generator

ActiveLibrary

earc/data-primary-key-generator
===============================

eArc - the explicit architecture framework - data primary key generator component

0.0(5y ago)0161MITPHPPHP ^8.0

Since Apr 7Pushed 5y ago1 watchersCompare

[ Source](https://github.com/Koudela/eArc-data-primary-key-generator)[ Packagist](https://packagist.org/packages/earc/data-primary-key-generator)[ RSS](/packages/earc-data-primary-key-generator/feed)WikiDiscussions master Synced today

READMEChangelog (1)Dependencies (4)Versions (2)Used By (1)

eArc-data-primary-key-generator
===============================

[](#earc-data-primary-key-generator)

Primary key generator for the [earc/data](https://github.com/Koudela/eArc-data)abstraction.

table of contents
-----------------

[](#table-of-contents)

- [installation](#installation)
- [basic usage](#basic-usage)
    - [bootstrapping the primary key generator](#bootstrapping-the-primary-key-generator)
        - [using a redis server](#using-a-redis-server)
        - [using the filesystem](#using-the-filesystem)
    - [determine the key generation strategy](#determine-the-key-generation-strategy)
- [advanced usage](#advanced-usage)
    - [naming of the redis hash key](#naming-of-the-redis-hash-key)
    - [naming of the filesystem directory](#naming-of-the-filesystem-directory)
- [releases](#releases)
    - [release 0.0](#release-00)

installation
------------

[](#installation)

Install the earc/data-primary-key-generator library via composer.

```
$ composer require earc/data-primary-key-generator
```

basic usage
-----------

[](#basic-usage)

### bootstrapping the primary key generator

[](#bootstrapping-the-primary-key-generator)

Initialize the earc/data abstraction in your index.php, bootstrap or configuration script.

```
use eArc\Data\Initializer;

Initializer::init();
```

Then register the earc/data-primary-key-generator to the earc/data `onAutoPrimaryKey`event.

```
use eArc\Data\ParameterInterface;
use eArc\DataPrimaryKeyGenerator\PrimaryKeyGenerator;

di_tag(ParameterInterface::TAG_ON_AUTO_PRIMARY_KEY, PrimaryKeyGenerator::class);
```

Now earc/data is ready to use earc/data-primary-key-generator to generate UUIDs as primary keys for your entities.

If you want to generate incremental primary keys, you have to decide where to cache the maximal keys of the entity classes. You can choose between the filesystem and a redis server.

#### using a redis server

[](#using-a-redis-server)

To use the redis server, set the infrastructure parameter to `USE_REDIS`.

```
use eArc\DataPrimaryKeyGenerator\ParameterInterface;
use eArc\DataPrimaryKeyGenerator\PrimaryKeyGenerator;

di_set_param(ParameterInterface::INFRASTRUCTURE, PrimaryKeyGenerator::USE_REDIS);
```

By default, earc/data-primary-key-generator uses `localhost` and the defaults of the php-redis-extension. You can overwrite these defaults:

```
use eArc\DataPrimaryKeyGenerator\ParameterInterface;

di_set_param(ParameterInterface::REDIS_CONNECTION, ['127.0.0.1', 6379]);
```

This array is handed to the `Redis::connect()` method as arguments. Consult the [phpredis documentation](https://github.com/phpredis/phpredis/#connect-open) for valid values and configuration options.

Now earc/data is ready to use the earc/data-primary-key-generator to generate incremental primary keys for your entities.

#### using the filesystem

[](#using-the-filesystem)

To use the filesystem, set the infrastructure parameter to `USE_FILESYSTEM`.

```
use eArc\DataPrimaryKeyGenerator\ParameterInterface;
use eArc\DataPrimaryKeyGenerator\PrimaryKeyGenerator;

di_set_param(ParameterInterface::INFRASTRUCTURE, PrimaryKeyGenerator::USE_FILESYSTEM);
```

Then configure the data filesystem path for the [earc/data-filesystem](https://github.com/Koudela/eArc-data-filesystem) bridge.

```
use eArc\DataFilesystem\ParameterInterface;

di_set_param(ParameterInterface::DATA_PATH, '/path/to/save/the/entity/data');
```

Now earc/data is ready to use the earc/data-primary-key-generator to generate incremental primary keys for your entities.

### determine the key generation strategy

[](#determine-the-key-generation-strategy)

There are two supported primary key generation strategies.

1. using [UUIDs](https://de.wikipedia.org/wiki/Universally_Unique_Identifier)
2. incrementing a positive integer for each entity class

Each has its own advantages and downsides:

1. The UUIDs are globally unique.
2. The incremented integer keys require less space and give the entities a natural order, but this strategy requires an infrastructure to cache the maximal primary key for the classes.

The key generation strategy can be determined individually by implementing the `AutoincrementPrimaryKeyInterface` or the `AutoUUIDPrimaryKeyInterface` in the entity class.

```
use eArc\Data\Entity\AbstractEntity;
use eArc\DataPrimaryKeyGenerator\AutoincrementPrimaryKeyInterface;
use eArc\DataPrimaryKeyGenerator\AutoUUIDPrimaryKeyInterface;

class MyEntityUUID extends AbstractEntity implements AutoUUIDPrimaryKeyInterface
{
    public function setPrimaryKey(?string $primaryKey): void
    {
        $this->primaryKey = $primaryKey;
    }
}

class MyEntityAutoincrementPK extends AbstractEntity implements AutoincrementPrimaryKeyInterface
{
    public function setPrimaryKey(?string $primaryKey): void
    {
        $this->primaryKey = $primaryKey;
    }
}
```

Or it can be determined globally by setting the `DEFAULT_INTERFACE` parameter:

```
use eArc\DataPrimaryKeyGenerator\AutoincrementPrimaryKeyInterface;
use eArc\DataPrimaryKeyGenerator\ParameterInterface;

di_set_param(ParameterInterface::DEFAULT_INTERFACE, AutoincrementPrimaryKeyInterface::class);
```

This provides a fallback if no interface is present. Of course the `AutoPrimaryKeyInterface`of the earc/data library has to be implemented to trigger the `onAutoPrimaryKey`event.

```
use eArc\Data\Entity\AbstractEntity;
use eArc\Data\Entity\Interfaces\PrimaryKey\AutoPrimaryKeyInterface;

class MyEntityAutoincrementPK extends AbstractEntity implements AutoPrimaryKeyInterface
{
    public function setPrimaryKey(?string $primaryKey): void
    {
        $this->primaryKey = $primaryKey;
    }
}
```

advanced usage
--------------

[](#advanced-usage)

### naming of the redis hash key

[](#naming-of-the-redis-hash-key)

If you use the increment strategy together with the redis server, earc/data-primary-key-generator uses [redis hashes](https://redis.io/commands#hash)to cache the maximal keys of the entity classes. By default, the hash-key is named `earc-data-pk-gen`. If you need another name to manage the redis namespace, you can overwrite the default:

```
use eArc\DataPrimaryKeyGenerator\ParameterInterface;

di_set_param(ParameterInterface::HASH_KEY_NAME, 'my-hash-key-name');
```

### naming of the filesystem directory

[](#naming-of-the-filesystem-directory)

If you use the increment strategy together with the filesystem, earc/data-primary-key-generator uses the `@earc-data-pk-gen` postfix to extend the filesystem entity path of earc/data-filesystem to cache the maximal primary key of the entity class. You can change this by setting the `DIR_NAME_POSTFIX`parameter.

```
use eArc\DataPrimaryKeyGenerator\ParameterInterface;

di_set_param(ParameterInterface::DIR_NAME_POSTFIX, '@my-dir-name-postfix');
```

releases
--------

[](#releases)

### release 0.0

[](#release-00)

- the first official release
- PHP ^8.0

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

 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.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

1859d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/b7308f2797252cace014cfec12c1b5978c1bf5608be78d7a188ff690192959f3?d=identicon)[Thomas Koudela](/maintainers/Thomas%20Koudela)

---

Top Contributors

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

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/earc-data-primary-key-generator/health.svg)

```
[![Health](https://phpackages.com/badges/earc-data-primary-key-generator/health.svg)](https://phpackages.com/packages/earc-data-primary-key-generator)
```

PHPackages © 2026

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