PHPackages                             rlmckenney/yii-random-key - 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. rlmckenney/yii-random-key

ActiveYii-extension[Utility &amp; Helpers](/categories/utility)

rlmckenney/yii-random-key
=========================

A Yii Framework behavior class to generate UIDs of a given integer length and target data type.

v1.1.0(12y ago)025MITPHP

Since Aug 25Pushed 12y ago1 watchersCompare

[ Source](https://github.com/rlmckenney/yii-random-key)[ Packagist](https://packagist.org/packages/rlmckenney/yii-random-key)[ RSS](/packages/rlmckenney-yii-random-key/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (2)DependenciesVersions (3)Used By (0)

Yii RandomKey
=============

[](#yii-randomkey)

A Yii Framework behavior class to generate UIDs of a given integer length and target data type.

This is a behavior extension for the Yii Framework version 1.1.x

It adds a method to generate a random key of a desired number of digits for use as a primary key in your database. This will give you the security obfuscation advantages of a non-sequential primary key when it will be exposed to the user, but still retain the performance of an integer based primary key value.

**UPDATED:**There is a new method `getNewId($className)`. This should be the main method called to use this behaviour with a given class. It uses the existing getRandomKey() method and will test the randomKey generated against the existing ids in the table for the given class. If there is a conflict, it will generate another key to test and only return a value when there is no conflict.

The following target data types have the associated maximum digits:

```
'TINYINT'   =>  8 bit - Max  3 digits
'SMALLINT'  => 16 bit - Max  5 digits
'MEDIUMINT' => 24 bit - Max  8 digits
'INT'       => 32 bit - Max 10 digits
'BIGINT'    => 64 bit - Max 18 digits (see note)
```

**NOTE:**PHP does not support unsigned integers, so the max value for BIGINT is half of the full range on MySQL. If you want to use the full range of BIG\_INT, I recommend that you use the UUID\_SHORT() function in MySQL. [http://dev.mysql.com/doc/refman/5.6/en/miscellaneous-functions.html#function\_uuid-short](http://dev.mysql.com/doc/refman/5.6/en/miscellaneous-functions.html#function_uuid-short)

It will automatically detect if you are running on a 32 bit OS and impose reduced limits. BIGINT will not be available and INT max digits = 9 with max value of 999999999 instead of the full 4294967295.

Usage
-----

[](#usage)

The behavior can be attached to a specific model, controller, or component. To make it easily accessible application wide, you may wish to attach it to your application's db component.

Add the following parameters to the components section of your config

```
'components' => array(
  'db'=>array(
      ...

      'behaviors'=>array(
        'uid'=>array(
            'class'    => 'vendor.rlmckenney.yii-random-key.RandomKeyBehavior',
            'dataType' => 'MEDIUMINT',  // optionally set your own default property values
            'digits'   => 8,            // optionally set your own default property values
            ),
        ),
    ),
),
```

If you omit the dataType and digits property values from the config, the behavior will default to

```
'dataType' => 'INT'
'digits'   => 10
```

Then in your model when you need to generate a new primary key, simply call the getNewID() method:

```
$id = Yii::app()->db->uid->getNewId('User');  // gets new key value and tests for conflicts with
                                              // existing IDs in the table for the given class name
                                              // in this example 'User'.
```

Or, to override your defaults, call it like this:

```
Yii::app()->db->uid->dataType   = 'BIGINT';  // optionally override the default property values
Yii::app()->db->uid->digits     = 15;        // optionally override the default property values
$id = Yii::app()->db->uid->getNewId('User');
```

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity64

Established project with proven stability

 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 ~131 days

Total

2

Last Release

4513d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/c03e2ae96ba5ab241cabed628d8e992077372a7211b7cb531bfb357383ec35ff?d=identicon)[rlmckenney](/maintainers/rlmckenney)

---

Top Contributors

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

---

Tags

randomintegerUIDprimary key

### Embed Badge

![Health badge](/badges/rlmckenney-yii-random-key/health.svg)

```
[![Health](https://phpackages.com/badges/rlmckenney-yii-random-key/health.svg)](https://phpackages.com/packages/rlmckenney-yii-random-key)
```

###  Alternatives

[paragonie/random_compat

PHP 5.x polyfill for random\_bytes() and random\_int() from PHP 7

8.2k655.0M405](/packages/paragonie-random-compat)[brick/math

Arbitrary-precision arithmetic library

2.1k504.0M277](/packages/brick-math)[symfony/uid

Provides an object-oriented API to generate and represent UIDs

610280.0M754](/packages/symfony-uid)[ircmaxell/random-lib

A Library For Generating Secure Random Numbers

84130.2M119](/packages/ircmaxell-random-lib)[mistic100/randomcolor

Generate attractive random colors

2431.4M6](/packages/mistic100-randomcolor)[pragmarx/random

Create random chars, numbers, strings

714.2M5](/packages/pragmarx-random)

PHPackages © 2026

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