ArticleLike   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
eloc 20
c 2
b 0
f 0
dl 0
loc 64
ccs 0
cts 46
cp 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A behaviors() 0 10 1
A tableName() 0 3 1
A getArticle() 0 3 1
A getUser() 0 3 1
A findByArticleIdAndUserId() 0 5 1
A rules() 0 7 1
A attributeLabels() 0 6 1
1
<?php
2
3
namespace App\Model;
4
5
use App\Behavior\CreatorBehavior;
6
use App\Behavior\TimestampBehavior;
7
use Yii;
8
9
/**
10
 * This is the model class for table "{{%article_like}}".
11
 *
12
 * @property int $article_id Article ID
13
 * @property int $user_id User ID
14
 * @property int $create_time Create Time
15
 *
16
 * @property Article $article
17
 * @property User $user
18
 */
19
class ArticleLike extends ActiveRecord
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public static function tableName()
25
    {
26
        return '{{%article_like}}';
27
    }
28
29
    public function behaviors()
30
    {
31
        return [
32
            [
33
                'class' => TimestampBehavior::class,
34
                'updatedAtAttribute' => null,
35
            ],
36
            [
37
                'class' => CreatorBehavior::class,
38
                'creatorAttribute' => 'user_id',
39
            ],
40
        ];
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function rules()
47
    {
48
        return [
49
            [['article_id'], 'required'],
50
            [['article_id', 'create_time'], 'integer'],
51
            // [['article_id', 'user_id'], 'unique', 'targetAttribute' => ['article_id', 'user_id']],
52
            ['article_id', 'exist', 'targetClass' => Article::class, 'targetAttribute' => 'id'],
53
        ];
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function attributeLabels()
60
    {
61
        return [
62
            'article_id' => Yii::t('app', 'Article ID'),
63
            'user_id' => Yii::t('app', 'User ID'),
64
            'create_time' => Yii::t('app', 'Create Time'),
65
        ];
66
    }
67
68
    public function getArticle()
69
    {
70
        return $this->hasOne(Article::class, ['id' => 'article_id']);
71
    }
72
73
    public function getUser()
74
    {
75
        return $this->hasOne(User::class, ['id' => 'user_id']);
76
    }
77
78
    public static function findByArticleIdAndUserId(int $articleId, int $userId): ?self
79
    {
80
        return static::findOne([
0 ignored issues
show
Bug Best Practice introduced by
The expression return static::findOne(a... 'user_id' => $userId)) returns the type yii\db\ActiveRecord which includes types incompatible with the type-hinted return App\Model\ArticleLike|null.
Loading history...
81
            'article_id' => $articleId,
82
            'user_id' => $userId,
83
        ]);
84
    }
85
}
86