-
Notifications
You must be signed in to change notification settings - Fork 2
/
MdmAutonumberBehavior.php
49 lines (45 loc) · 1.19 KB
/
MdmAutonumberBehavior.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
40
41
42
43
44
45
46
47
48
49
<?php
Yii::import('ext.autonumber.MdmAutonumber');
/**
* Description of MdmAutonumberBehavior
*
* @author MDMunir
*/
class MdmAutonumberBehavior extends CActiveRecordBehavior
{
public $attribute;
public $group;
public $value;
public $digit;
/**
*
* @param CModelEvent $event
*/
protected function beforeSave($event)
{
if ($event->sender->isNewRecord) {
$event->sender->{$this->attribute} = $this->getNumber();
}
}
private function getNumber()
{
$ar = MdmAutonumber::model()->findByPk(array(
'template_group' => $this->group,
'template_num' => $this->value,
));
if ($ar) {
$number = $ar->auto_number + 1;
} else {
$ar = new MdmAutonumber;
$ar->template_group = $this->group;
$ar->template_num = $value;
$number = 1;
}
$ar->update_time = time();
$ar->auto_number = $number;
if (!$ar->save()) {
throw new CException(implode("\n", $ar->errors));
}
return str_replace('?', $this->digit ? sprintf("%0{$this->digit}d", $number) : $number, $this->value);
}
}