|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace andmemasin\myabstract; |
|
4
|
|
|
|
|
5
|
|
|
use andmemasin\myabstract\traits\ConsoleAwareTrait; |
|
6
|
|
|
use yii; |
|
7
|
|
|
use andmemasin\myabstract\traits\ModuleTrait; |
|
8
|
|
|
use andmemasin\myabstract\traits\MyActiveTrait; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* A wrapper class do have all models with custom features |
|
12
|
|
|
* |
|
13
|
|
|
* @property User $userCreated |
|
14
|
|
|
* @property User $userUpdated |
|
15
|
|
|
* @property User $userClosed |
|
16
|
|
|
* |
|
17
|
|
|
* @package app\models\myabstract |
|
18
|
|
|
* @author Tonis Ormisson <[email protected]> |
|
19
|
|
|
*/ |
|
20
|
|
|
class MyActiveRecord extends ActiveRecord |
|
21
|
|
|
{ |
|
22
|
|
|
|
|
23
|
|
|
use MyActiveTrait; |
|
|
|
|
|
|
24
|
|
|
use ModuleTrait; |
|
25
|
|
|
use ConsoleAwareTrait; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Get User who created the record |
|
29
|
|
|
* @return User |
|
30
|
|
|
*/ |
|
31
|
|
|
public function getUserCreated() { |
|
32
|
|
|
/** @var User $userClassName */ |
|
33
|
|
|
$userClassName = $this->getAbstractModule()->userClassName; |
|
34
|
|
|
return $userClassName::findOne($this->{$this->userCreatedCol}); |
|
35
|
|
|
} |
|
36
|
|
|
/** |
|
37
|
|
|
* Get User who last updated the record |
|
38
|
|
|
* @return User |
|
39
|
|
|
*/ |
|
40
|
|
|
public function getUserUpdated() { |
|
41
|
|
|
/** @var User $userClassName */ |
|
42
|
|
|
$userClassName = $this->getAbstractModule()->userClassName; |
|
43
|
|
|
return $userClassName::findOne($this->{$this->userUpdatedCol}); |
|
44
|
|
|
} |
|
45
|
|
|
/** |
|
46
|
|
|
* Get User who last closed (deleted) the record |
|
47
|
|
|
* @return User |
|
48
|
|
|
*/ |
|
49
|
|
|
public function getUserClosed() { |
|
50
|
|
|
/** @var User $userClassName */ |
|
51
|
|
|
$userClassName = $this->getAbstractModule()->userClassName; |
|
52
|
|
|
return $userClassName::findOne($this->{$this->userClosedCol}); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Get Time record was created |
|
57
|
|
|
* @return String datetime(6) |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getTimeCreated() { |
|
60
|
|
|
return $this->{$this->timeCreatedCol}; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get Time record was updated |
|
65
|
|
|
* @return String datetime(6) |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getTimeUpdated() { |
|
68
|
|
|
return $this->{$this->timeUpdatedCol}; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Get Time record was closed (deleted) |
|
73
|
|
|
* @return String datetime(6) |
|
74
|
|
|
*/ |
|
75
|
|
|
public function getTimeClosed() { |
|
76
|
|
|
return $this->{$this->timeClosedCol}; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param string $className |
|
82
|
|
|
* @param string $idColumn |
|
83
|
|
|
* @param array $filters |
|
84
|
|
|
* @return mixed |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getRelationCount($className, $idColumn = null, $filters = null) { |
|
87
|
|
|
if (!$idColumn) { |
|
88
|
|
|
$idColumn = $this->tableName() . "_id"; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$config = [ |
|
92
|
|
|
'class' => $className, |
|
93
|
|
|
]; |
|
94
|
|
|
|
|
95
|
|
|
/** @var MyActiveRecord $model */ |
|
96
|
|
|
$model = Yii::createObject($config); |
|
97
|
|
|
/** @var ActiveRecord $className */ |
|
98
|
|
|
$query = $model->query() |
|
99
|
|
|
->from($className::tableName()) |
|
100
|
|
|
->andWhere([$idColumn => $this->primaryKey]); |
|
101
|
|
|
if ($filters) { |
|
102
|
|
|
$query->andWhere($filters); |
|
103
|
|
|
} |
|
104
|
|
|
return $query->count(); |
|
105
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @param mixed $condition |
|
110
|
|
|
* @return static|null |
|
111
|
|
|
*/ |
|
112
|
|
|
public static function findOne($condition) |
|
113
|
|
|
{ |
|
114
|
|
|
/** |
|
115
|
|
|
* primary keys must always be integers and we cast the param to int |
|
116
|
|
|
* for input sanitizing if its not an array |
|
117
|
|
|
*/ |
|
118
|
|
|
if(!is_array($condition)) { |
|
119
|
|
|
return parent::findOne((int) $condition); |
|
120
|
|
|
} |
|
121
|
|
|
return parent::findOne($condition); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* {@inheritdoc} |
|
127
|
|
|
* @return static[] an array of ActiveRecord instances, or an empty array if nothing matches. |
|
128
|
|
|
*/ |
|
129
|
|
|
public static function findAll($condition) |
|
130
|
|
|
{ |
|
131
|
|
|
/** |
|
132
|
|
|
* primary keys must always be integers and we cast the param to int |
|
133
|
|
|
* for input sanitizing if its not an array |
|
134
|
|
|
*/ |
|
135
|
|
|
if(!is_array($condition)) { |
|
136
|
|
|
return parent::findAll((int) $condition); |
|
137
|
|
|
} |
|
138
|
|
|
return parent::findAll($condition); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
} |
|
142
|
|
|
|