razonyang /
yii2-app-template
| 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
Loading history...
|
|||
| 81 | 'article_id' => $articleId, |
||
| 82 | 'user_id' => $userId, |
||
| 83 | ]); |
||
| 84 | } |
||
| 85 | } |
||
| 86 |