Comment.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace app\common\model;
  3. use think\Collection;
  4. use think\Model;
  5. use traits\model\SoftDelete;
  6. /**
  7. * 短信验证码
  8. * @property Collection|Model commentable
  9. */
  10. class Comment Extends Model
  11. {
  12. use SoftDelete;
  13. protected $deleteTime='deleted_at';
  14. // 开启自动写入时间戳字段
  15. protected $autoWriteTimestamp = true;
  16. protected $createTime='created_at';
  17. protected $updateTime=false;
  18. protected $hidden=[
  19. 'deleted_at',
  20. 'commentable_id',
  21. ];
  22. public static $alias=[
  23. 'video'=>Video::class,
  24. ];
  25. protected $append=[
  26. ];
  27. public function commentable(){
  28. return $this->morphTo('commentable',self::$alias);
  29. }
  30. public function user(){
  31. return $this->belongsTo(User::class);
  32. }
  33. /** system */
  34. protected static function init()
  35. {
  36. self::afterInsert(function (self $comment){
  37. if($comment->commentable_type=='video'){
  38. $info=Video::find($comment->commentable_id);
  39. $info && $info->setInc('comment_num');
  40. }
  41. });
  42. self::afterDelete(function (self $comment){
  43. });
  44. }
  45. }