-
Notifications
You must be signed in to change notification settings - Fork 20
/
OwnerBehavior.php
39 lines (27 loc) · 1016 Bytes
/
OwnerBehavior.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
<?php
// Owner Behavior by thyseus <thyseus@gmail.com>
// Assuming a dataset is "owned" by a user, we need to set the id
// of the current logged in user when saving the dataset automatically.
// Optional, a "last updated by" column can save the user that last updated
// the dataset. The Owner will never be touched.
class OwnerBehavior extends CActiveRecordBehavior {
/**
* The field that stores the pk of the owner
*/
public $ownerColumn = 'owner_id';
/**
* The field that stores the pk of user that did the the last change
*/
public $lastChangeColumn = 'last_change_by';
public function beforeValidate($on) {
if(isset($this->owner->tableSchema->columns[$this->ownerColumn]))
if ($this->owner->isNewRecord)
$this->owner->{$this->ownerColumn} = Yii::app()->user->id;
return true;
}
public function beforeSave($on) {
if(isset($this->owner->tableSchema->columns[$this->lastChangeColumn]))
$this->owner->{$this->lastChangeColumn} = Yii::app()->user->id;
return true;
}
}