2017 © Pedro Peláez
 

yii-extension yii-random-key

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

image

rlmckenney/yii-random-key

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

  • Sunday, January 19, 2014
  • by rlmckenney
  • Repository
  • 1 Watchers
  • 0 Stars
  • 16 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

Yii RandomKey

A Yii Framework behavior class to generate UIDs of a given integer length and target data type., (*1)

This is a behavior extension for the Yii Framework version 1.1.x, (*2)

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., (*3)

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., (*4)

The following target data types have the associated maximum digits:, (*5)

'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, (*6)

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., (*7)

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., (*8)

Add the following parameters to the components section of your config, (*9)

'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, (*10)

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

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

$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:, (*12)

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');

The Versions

19/01 2014

dev-master

9999999-dev

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

  Sources   Download

MIT

random integer uid primary key

04/01 2014

v1.1.0

1.1.0.0

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

  Sources   Download

MIT

random integer uid primary key

25/08 2013

v1.0.0

1.0.0.0

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

  Sources   Download

MIT

random integer uid primary key