Passed
Branch master (b38eac)
by Tõnis
03:12
created

MyActiveRecord::findAll()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 10
rs 10
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;
0 ignored issues
show
introduced by
The trait andmemasin\myabstract\traits\MyActiveTrait requires some properties which are not provided by andmemasin\myabstract\MyActiveRecord: $db, $cache, $identity, $user
Loading history...
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
    /** {@inheritDoc} */
109
    public static function findOne($condition)
110
    {
111
        /**
112
         * primary keys must always be integers and we cast the param to int
113
         * for input sanitizing if its not an array
114
         */
115
        if(!is_array($condition)) {
116
            return parent::findOne((int) $condition);
117
        }
118
        return parent::findOne($condition);
119
    }
120
121
122
    /** {@inheritDoc} */
123
    public static function findAll($condition)
124
    {
125
        /**
126
         * primary keys must always be integers and we cast the param to int
127
         * for input sanitizing if its not an array
128
         */
129
        if(!is_array($condition)) {
130
            return parent::findAll((int) $condition);
131
        }
132
        return parent::findAll($condition);
133
    }
134
135
}
136