for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
/**
* @link http://www.tintsoft.com/
* @copyright Copyright (c) 2012 TintSoft Technology Co. Ltd.
* @license http://www.tintsoft.com/license/
*/
namespace yuncms\behaviors;
use yii\base\InvalidConfigException;
use yii\behaviors\AttributeBehavior;
use yii\db\BaseActiveRecord;
use yii\di\Instance;
use yuncms\base\Snowflake;
* Class SnowflakeBehavior
*
* @author Tongle Xu <[email protected]>
* @since 3.0
class SnowflakeBehavior extends AttributeBehavior
{
* @var string
public $attribute = 'id';
* @inheritdoc
public $value;
* @var string|Snowflake
protected $snowflake = 'snowflake';
public function init()
parent::init();
if (empty($this->attributes)) {
$this->attributes = [BaseActiveRecord::EVENT_BEFORE_INSERT => [$this->attribute],];
}
if ($this->attribute === null) {
throw new InvalidConfigException('Either "attribute" property must be specified.');
$this->snowflake = Instance::ensure($this->snowflake, 'yuncms\base\Snowflake');
protected function getValue($event)
if ($this->value === null) {
return $this->snowflake->next();
return parent::getValue($event);