1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace andmemasin\myabstract; |
4
|
|
|
|
5
|
|
|
use yii; |
6
|
|
|
use andmemasin\myabstract\traits\ModuleTrait; |
7
|
|
|
use andmemasin\myabstract\traits\MyActiveTrait; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* A wrapper class do have all models with custom features |
11
|
|
|
* |
12
|
|
|
* @property User $userCreated |
13
|
|
|
* @property User $userUpdated |
14
|
|
|
* @property User $userClosed |
15
|
|
|
* @property boolean $isConsole whether we currently run in console app or not |
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
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Get User who created the record |
28
|
|
|
* @return User |
29
|
|
|
*/ |
30
|
|
|
public function getUserCreated() { |
31
|
|
|
/** @var User $userClassName */ |
32
|
|
|
$userClassName = $this->getAbstractModule()->userClassName; |
33
|
|
|
return $userClassName::findOne($this->{$this->userCreatedCol}); |
34
|
|
|
} |
35
|
|
|
/** |
36
|
|
|
* Get User who last updated the record |
37
|
|
|
* @return User |
38
|
|
|
*/ |
39
|
|
|
public function getUserUpdated() { |
40
|
|
|
/** @var User $userClassName */ |
41
|
|
|
$userClassName = $this->getAbstractModule()->userClassName; |
42
|
|
|
return $userClassName::findOne($this->{$this->userUpdatedCol}); |
43
|
|
|
} |
44
|
|
|
/** |
45
|
|
|
* Get User who last closed (deleted) the record |
46
|
|
|
* @return User |
47
|
|
|
*/ |
48
|
|
|
public function getUserClosed() { |
49
|
|
|
/** @var User $userClassName */ |
50
|
|
|
$userClassName = $this->getAbstractModule()->userClassName; |
51
|
|
|
return $userClassName::findOne($this->{$this->userClosedCol}); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get Time record was created |
56
|
|
|
* @return String datetime(6) |
57
|
|
|
*/ |
58
|
|
|
public function getTimeCreated() { |
59
|
|
|
return $this->{$this->timeCreatedCol}; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get Time record was updated |
64
|
|
|
* @return String datetime(6) |
65
|
|
|
*/ |
66
|
|
|
public function getTimeUpdated() { |
67
|
|
|
return $this->{$this->timeUpdatedCol}; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get Time record was closed (deleted) |
72
|
|
|
* @return String datetime(6) |
73
|
|
|
*/ |
74
|
|
|
public function getTimeClosed() { |
75
|
|
|
return $this->{$this->timeClosedCol}; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param string $className |
81
|
|
|
* @param string $idColumn |
82
|
|
|
* @param array $filters |
83
|
|
|
* @return mixed |
84
|
|
|
*/ |
85
|
|
|
public function getRelationCount($className, $idColumn = null, $filters = null) { |
86
|
|
|
if (!$idColumn) { |
87
|
|
|
$idColumn = $this->tableName() . "_id"; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$config = [ |
91
|
|
|
'class' => $className, |
92
|
|
|
]; |
93
|
|
|
|
94
|
|
|
/** @var MyActiveRecord $model */ |
95
|
|
|
$model = Yii::createObject($config); |
96
|
|
|
/** @var ActiveRecord $className */ |
97
|
|
|
$query = $model->query() |
98
|
|
|
->from($className::tableName()) |
99
|
|
|
->andWhere([$idColumn => $this->primaryKey]); |
100
|
|
|
if ($filters) { |
101
|
|
|
$query->andWhere($filters); |
102
|
|
|
} |
103
|
|
|
return $query->count(); |
104
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return boolean |
109
|
|
|
*/ |
110
|
|
|
public function getIsConsole() |
111
|
|
|
{ |
112
|
|
|
return Yii::$app instanceof yii\console\Application; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
|
116
|
|
|
} |