Completed
Push — master ( c75724...87d358 )
by Razon
03:04
created

ArticleLike::attributeLabels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
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', 'user_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
            ['user_id', 'exist', 'targetClass' => User::class, 'targetAttribute' => 'id'],
54
        ];
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function attributeLabels()
61
    {
62
        return [
63
            'article_id' => Yii::t('app', 'Article ID'),
64
            'user_id' => Yii::t('app', 'User ID'),
65
            'create_time' => Yii::t('app', 'Create Time'),
66
        ];
67
    }
68
69
    public function getArticle()
70
    {
71
        return $this->hasOne(Article::class, ['id' => 'article_id']);
72
    }
73
74
    public function getUser()
75
    {
76
        return $this->hasOne(User::class, ['id' => 'user_id']);
77
    }
78
79
    public static function findByArticleIdAndUserId(int $articleId, int $userId): ?self
80
    {
81
        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...
82
            'article_id' => $articleId,
83
            'user_id' => $userId,
84
        ]);
85
    }
86
}
87