PHPackages                             philippgrashoff/atkdatamodeltraits - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. philippgrashoff/atkdatamodeltraits

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

philippgrashoff/atkdatamodeltraits
==================================

A small collection of traits to be used with Atk4\\Data\\Model

6.0.0(1y ago)1684↓31%4PHPPHP 8.\*CI failing

Since Aug 13Pushed 1y ago1 watchersCompare

[ Source](https://github.com/PhilippGrashoff/atkdatamodeltraits)[ Packagist](https://packagist.org/packages/philippgrashoff/atkdatamodeltraits)[ RSS](/packages/philippgrashoff-atkdatamodeltraits/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (8)Dependencies (3)Versions (9)Used By (4)

atkdatamodeltraits
==================

[](#atkdatamodeltraits)

[![codecov](https://camo.githubusercontent.com/7873d63e7021b74abed7a0ff6e568bc08b3e0c3bd50e3643309b47e34d028822/68747470733a2f2f636f6465636f762e696f2f67682f5068696c69707047726173686f66662f61746b646174616d6f64656c7472616974732f6272616e63682f6d61696e2f67726170682f62616467652e737667)](https://codecov.io/gh/PhilippGrashoff/atkdatamodeltraits)

A small collection of traits to be used with [Atk4\\Data\\Model](https://github.com/atk4/data/blob/develop/src/Model.php).

CreatedDateAndLastUpdatedTrait
==============================

[](#createddateandlastupdatedtrait)

This trait can be used to add a `created_date` and/or a `last_updated` field to a model. The corresponding hooks are automatically added to save `created_date` on insert and `last_updated` on update.

```
class SomeModel extends Model
{
    use CreatedDateAndLastUpdatedTrait;

    protected function init(): void
    {
        parent::init();
        $this->addCreatedDateFieldAndHook();
        $this->addLastUpdatedFieldAndHook();
    }
}
```

UniqueFieldTrait
================

[](#uniquefieldtrait)

This trait provides a function `isFieldUnique()` to check if the field's current value is unique among all records in persistence. This functionality can also be achieved by adding a UNIQUE index to an SQL persistence. Having this functionality in the application layer can be sensible to be independent of the persistence's features - or to avoid getting back an exception from the persistence if a non-unique value is being tried to save.

```
class SomeModel extends Model
{
    use UniqueFieldTrait;

    protected function init(): void
    {
        parent::init();
        $this->addField('unique_field');
    }

    $this->onHook(
        Model::HOOK_BEFORE_SAVE,
        function(self $entity, bool $isUpdate) {
            while(!$entity->isFieldUnique('unique_field')) {
                //some function which recalculates field value.
            }
        }
    );
}
```

CryptIdTrait
============

[](#cryptidtrait)

This trait is used to generate cryptic IDs like `D6f2-a395Jskv2`. You can freely define the format the cryptic ID shall have. The functionality can for example be used to create coupon codes or create unguessable, yet human-readable codes/identifiers. The characters I,l,0 and O are removed as they can be easily be mistaken by humans. To use this trait, you just need to add the cryptic ID field in `Model::init()` using `addCryptIdFieldAndHooks` and implement a custom method `generateCryptId()` that returns a random string in the format of your choice:

```
class SomeModel extends Model
{
    use CryptIdTrait;

    protected function init(): void
    {
        parent::init();
        $this->addCryptIdFieldAndHooks('crypt_id');
    }

    /**
     * Custom implementation if cryptic ID format, In this case XXXXX-XXXXX-XXXXX
     */
    protected function generateCryptId(): string
    {
        $cryptId = '';
        for ($i = 0; $i < 3; $i++) {
            if($i > 0) {
                $cryptId .= '-';
            }
            for ($j = 0; $j < 5; $j++) {
                $cryptId .= $this->getRandomChar();
            }
        }

        return $cryptId;
    }
}
```

###  Health Score

35

—

LowBetter than 79% of packages

Maintenance49

Moderate activity, may be stable

Popularity20

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity48

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

Every ~90 days

Recently: every ~145 days

Total

8

Last Release

370d ago

Major Versions

4.0.2 → 5.0.02023-10-02

5.2.2 → 6.0.02025-05-07

### Community

Maintainers

![](https://www.gravatar.com/avatar/b8d7dc9f818ca121a41b3d19bd263147b404e0488096d2357729dea14232ec6e?d=identicon)[Philipp Reisigl](/maintainers/Philipp%20Reisigl)

---

Top Contributors

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

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/philippgrashoff-atkdatamodeltraits/health.svg)

```
[![Health](https://phpackages.com/badges/philippgrashoff-atkdatamodeltraits/health.svg)](https://phpackages.com/packages/philippgrashoff-atkdatamodeltraits)
```

PHPackages © 2026

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