-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathUUIDBehavior.php
39 lines (33 loc) · 907 Bytes
/
UUIDBehavior.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
namespace mootensai\behaviors;
use yii\base\Behavior;
use yii\db\ActiveRecord;
/*
* UUID Behavior will set your ID with UUID
* @author Yohanes Candrajaya <[email protected]>
* @author Jiwantoro Ndaru <[email protected]>
*/
class UUIDBehavior extends Behavior {
/**
* Field/Column yang akan diisi UUID
* Default -> id
* @var type
*/
public $column = 'id';
/**
* Override event()
* memasukkan method beforeSave() kedalam komponen ActiveRecord::EVENT_BEFORE_INSERT
* @return type
*/
public function events() {
return[
ActiveRecord::EVENT_BEFORE_INSERT => 'beforeSave',
];
}
/**
* set beforeSave() -> UUID data
*/
public function beforeSave() {
$this->owner->{$this->column} = $this->owner->getDb()->createCommand("SELECT REPLACE(UUID(),'-','')")->queryScalar();
}
}