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([ |
|
|
|
|
82
|
|
|
'article_id' => $articleId, |
83
|
|
|
'user_id' => $userId, |
84
|
|
|
]); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|