Passed
Push — master ( d5d166...eae9c0 )
by Tõnis
02:28
created

MyActiveRecord::primaryKeySingle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
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
 *
16
 * @package app\models\myabstract
17
 * @author Tonis Ormisson <[email protected]>
18
 */
19
class MyActiveRecord extends ActiveRecord
20
{
21
22
    use MyActiveTrait;
0 ignored issues
show
introduced by
The trait andmemasin\myabstract\traits\MyActiveTrait requires some properties which are not provided by andmemasin\myabstract\MyActiveRecord: $db, $identity, $user
Loading history...
23
    use ModuleTrait;
24
25
26
    /** @var  array array or attribute & value pairs that will be assigned to all created children [['attributeName1'=>'defaultValue1'],['attributeNamen'=>'defaultValuen]] */
27
    public $defaultValues;
28
29
    public function __construct(array $config = [])
30
    {
31
        //assign defaultvalues
32
        if (!empty($config['defaultValues'])) {
33
            $this->defaultValues = $config['defaultValues'];
34
            $this->assignDefaultValues();
35
        }
36
        parent::__construct($config);
37
    }
38
39
40
41
    /**
42
     * Get User who created the record
43
     * @return User
44
     */
45
    public function getUserCreated() {
46
        /** @var User $userClassName */
47
        $userClassName = ModuleTrait::getModule()->userClassName;
48
        return $userClassName::findOne($this->{$this->userCreatedCol});
49
    }
50
    /**
51
     * Get User who last updated the record
52
     * @return User
53
     */
54
    public function getUserUpdated() {
55
        /** @var User $userClassName */
56
        $userClassName = ModuleTrait::getModule()->userClassName;
57
        return $userClassName::findOne($this->{$this->userUpdatedCol});
58
    }
59
    /**
60
     * Get User who last closed (deleted) the record
61
     * @return User
62
     */
63
    public function getUserClosed() {
64
        /** @var User $userClassName */
65
        $userClassName = ModuleTrait::getModule()->userClassName;
66
        return $userClassName::findOne($this->{$this->userClosedCol});
67
    }
68
69
    /**
70
     * Get Time record was created
71
     * @return String datetime(6)
72
     */
73
    public function getTimeCreated() {
74
        return $this->{$this->timeCreatedCol};
75
    }
76
77
    /**
78
     * Get Time record was updated
79
     * @return String datetime(6)
80
     */
81
    public function getTimeUpdated() {
82
        return $this->{$this->timeUpdatedCol};
83
    }
84
85
    /**
86
     * Get Time record was closed (deleted)
87
     * @return String datetime(6)
88
     */
89
    public function getTimeClosed() {
90
        return $this->{$this->timeClosedCol};
91
    }
92
93
    public function assignDefaultValues() {
94
        if (!empty($this->defaultValues)) {
95
            foreach ($this->defaultValues as $attribute =>$value) {
96
                $this->$attribute = $value;
97
            }
98
        }
99
    }
100
101
    /**
102
     * @param string $className
103
     * @param string $idColumn
104
     * @param array $filters
105
     * @return mixed
106
     */
107
    public function getRelationCount($className, $idColumn = null, $filters = null) {
108
        if (!$idColumn) {
109
            $idColumn = $this->tableName() . "_id";
110
        }
111
112
        $config = [
113
            'class' => $className,
114
        ];
115
116
        /** @var MyActiveRecord $model */
117
        $model = Yii::createObject($config);
118
        /** @var ActiveRecord $className */
119
        $query = $model->query()
120
            ->from($className::tableName())
121
            ->andWhere([$idColumn => $this->primaryKey]);
122
        if ($filters) {
123
            $query->andWhere($filters);
124
        }
125
        return $query->count();
126
127
    }
128
129
130
}