Completed
Push — master ( 1d1055...713dd8 )
by vistart
06:24
created

BaseBlameableModel::findByIdentity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link http://vistart.name/
9
 * @copyright Copyright (c) 2016 vistart
10
 * @license http://vistart.name/license/
11
 */
12
13
namespace vistart\Models\models;
14
15
use vistart\Models\traits\BlameableTrait;
16
17
/**
18
 * BaseBlameableEntityModel automatically fills the specified attributes with
19
 * the current user's GUID.
20
 * For example:<br/>
21
 * ~~~php
22
 * * @property string $comment
23
 * class Comment extends BaseBlameableEntityModel
24
 * {
25
 *     public static function tableName()
26
 *     {
27
 *         return <table_name>;
28
 *     }
29
 *     public function rules()
30
 *     {
31
 *         $rules = [
32
 *             [['comment'], 'required'],
33
 *             [['comment'], 'string', 'max' => 140],
34
 *         ];
35
 *         return array_merge(parent::rules(), $rules);
36
 *     }
37
 *     public function behaviors()
38
 *     {
39
 *         $behaviors = <Your Behaviors>;
40
 *         return array_merge(parent::behaviors(), $behaviors);
41
 *     }
42
 *     public function attributeLabels()
43
 *     {
44
 *         return [
45
 *             ...
46
 *         ];
47
 *     }
48
 * }
49
 * ~~~
50
 * Well, when you're signed-in, you can save a new `Example` instance:
51
 * ~~~php
52
 * $example = new Example();
53
 * $example->comment = 'New Comment.';
54
 * $example->save();
55
 * ~~~
56
 * or update an existing one:
57
 * ~~~php
58
 * $example = Example::find()
59
 *                   ->where([$this->createdByAttribute => $user_uuid])
60
 *                   ->one();
61
 * if ($example)
62
 * {
63
 *     $example->comment = 'Updated Comment.';
64
 *     $example->save();
65
 * }
66
 * ~~~
67
 * @property array createdByAttributeRules the whole validation rules of
68
 * creator attribute only, except of combination rules.
69
 * @property array updatedByAttributeRules the whole validation rules of
70
 * creator attribute only, except of combination rules.
71
 * @since 1.1
72
 * @version 2.0
73
 * @author vistart <[email protected]>
74
 */
75
abstract class BaseBlameableModel extends BaseEntityModel
76
{
77
    use BlameableTrait;
78
79
    /**
80
     * Initialize the blameable model.
81
     */
82 25
    public function init()
83
    {
84 25
        if (!is_string($this->queryClass)) {
85 19
            $this->queryClass = \vistart\Models\queries\BaseBlameableQuery::className();
86 19
        }
87 25
        if ($this->skipInit) {
88 17
            return;
89
        }
90 25
        $this->initBlameableEvents();
91 25
        parent::init();
92 25
    }
93
94
    /**
95
     * Get the query class.
96
     * @param \vistart\Models\models\BaseUserModel $identity
97
     * @return \vistart\Models\queries\BaseBlameableQuery
98
     */
99
    public static function findByIdentity($identity = null)
100
    {
101
        return static::find()->byIdentity($identity);
102
    }
103
}
104