PHP code example of bestyii / yii2-snowflake

1. Go to this page and download the library: Download bestyii/yii2-snowflake library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

bestyii / yii2-snowflake example snippets


return [
    //...
    'components' => [
        //...
        'snowflake' => [
            'class' => 'bestyii\snowflake\Snowflake',
            'startDate' => '2022-12-31',
        ],
    ],
];

return [
    //...
    'components' => [
        //...
        'snowflake' => [
            'class' => 'bestyii\snowflake\Snowflake',
            'startDate' => '1970-1-1',
            'datacenterId' => 11,
            'workerId' => 22,
            'sequencer' =>  '\bestyii\snowflake\RedisSequenceResolver', 
        ],
    ],
];

$id = \Yii::$app->snowflake->id();
// 401621641244704768

\Yii::$app->snowflake->parseId($id);
//[
//    'timestamp' => '1011001001011011000110000010011110010'
//    'sequence' => '000000000000'
//    'workerid' => '00000'
//    'datacenter' => '00000'
//]

public function behaviors()
{
    return [
        'snowflake' => [
            'class' => '\bestyii\snowflake\SnowflakeBehavior',
            'attributes' => 'id',// default:id
        ],
    ];
}

php composer.phar