1 <?php
2 // Owner Behavior by thyseus <thyseus@gmail.com>
3
4 // Assuming a dataset is "owned" by a user, we need to set the id
5 // of the current logged in user when saving the dataset automatically.
6 // Optional, a "last updated by" column can save the user that last updated
7 // the dataset. The Owner will never be touched.
8
9
10 class OwnerBehavior extends CActiveRecordBehavior {
11 /**
12 * The field that stores the pk of the owner
13 */
14 public $ownerColumn = 'owner_id';
15
16 /**
17 * The field that stores the pk of user that did the the last change
18 */
19 public $lastChangeColumn = 'last_change_by';
20
21 public function beforeValidate($on) {
22 if(isset($this->owner->tableSchema->columns[$this->ownerColumn]))
23 if ($this->owner->isNewRecord)
24 $this->owner->{$this->ownerColumn} = Yii::app()->user->id;
25
26
27 return true;
28 }
29
30 public function beforeSave($on) {
31
32 if(isset($this->owner->tableSchema->columns[$this->lastChangeColumn]))
33 $this->owner->{$this->lastChangeColumn} = Yii::app()->user->id;
34
35 return true;
36 }
37
38 }
39
40