This repository has been archived by the owner on Mar 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSActiveRecord.php
89 lines (83 loc) · 2.52 KB
/
NSActiveRecord.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
/**
* NSActiveRecord class file.
* @author Christoffer Niska <christoffer.niska@nordsoftware.com>
* @copyright Copyright © Nord Software 2013-
* @license http://www.opensource.org/licenses/bsd-license New BSD License
* @version 1.0.0
*/
/**
* The following properties are available for this model:
* @property string created
* @property string updated
* @property string deleted
* @property integer $status
*/
abstract class NSActiveRecord extends CActiveRecord {
// Active record statuses
const STATUS_DELETED = -1;
const STATUS_DEFAULT = 0;
/**
* Returns a list of behaviors that this model should behave as.
* @return array the behavior configurations (behavior name=>behavior configuration)
*/
public function behaviors() {
return array(
'formatter' => array(
'class' => 'vendor.crisu83.yii-formatter.behaviors.FormatterBehavior',
'formatters' => array(
'dateTime' => array('dateWidth' => 'short', 'timeWidth' => 'short'),
),
),
);
}
/**
* Returns the default named scope that should be implicitly applied to all queries for this model.
* @return array the query criteria.
*/
public function defaultScope() {
$scope = parent::defaultScope();
if ($this->hasAttribute('status')) {
$tableAlias = $this->getTableAlias(true, false/* do not check scopes */);
$condition = $tableAlias . '.status>=0';
$scope['condition'] = isset($scope['condition'])
? '(' . $scope['condition'] . ') AND (' . $condition . ')'
: $condition;
}
return $scope;
}
/**
* This method is invoked before saving a record (after validation, if any).
* @return boolean whether the saving should be executed. Defaults to true.
*/
protected function beforeSave() {
if ($this->isNewRecord) {
if ($this->hasAttribute('created')) {
$this->created = date('Y-m-d H:i:s');
}
} else {
if ($this->hasAttribute('updated')) {
$this->updated = date('Y-m-d H:i:s');
}
}
return parent::beforeSave(); // parent method must be called here in order for the audit behavior to work
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels() {
return array(
'created' => Yii::t('label', 'Created'),
'updated' => Yii::t('label', 'Last edit'),
'deleted' => Yii::t('label', 'Deleted'),
'status' => Yii::t('label', 'Status'),
);
}
/**
* Return basic select options for the record.
* @return array the options.
*/
public static function getSelectOptions() {
return CHtml::listData(static::model()->findAll(), 'id', 'name');
}
}