| 123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- <?php
- namespace common\behaviors;
- use common\helpers\snowflake\SnowFake;
- use yii\behaviors\AttributeBehavior;
- use yii\db\Expression;
- /**
- * Created by PhpStorm.
- * User: congli
- * Date: 2020/2/25
- * Time: 5:11 PM
- */
- class PrimaryKeyBehavior extends AttributeBehavior
- {
- public $primaryKeyAttribute = 'ID';
- public $value;
- public function init()
- {
- parent::init();
- if (empty($this->attributes)) {
- $this->attributes = [
- \yii\db\ActiveRecord::EVENT_BEFORE_INSERT => [$this->primaryKeyAttribute],
- ];
- }
- }
- /**
- * @param \yii\base\Event $event
- * @return mixed
- */
- protected function getValue($event)
- {
- //@todo 修正同一事务中取ID会出错的问题
- // return new Expression("REPLACE(UUID(),'-','')");
- // $uuidStr = \Yii::$app->db->createCommand('select UUID()')->queryScalar();
- // return str_replace('-', '', $uuidStr);
- return SnowFake::instance()->generateId();
- }
- }
|