Comments.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace app\models\base;
  3. use app\helpers\Transliterator;
  4. use yii\db\ActiveRecord;
  5. use yii\helpers\Url;
  6. /**
  7. * @property string $id
  8. * @property string $comment_pull_id
  9. * @property string $message
  10. * @property \app\models\News | null $post
  11. * @property string $publishedAt
  12. */
  13. class Comments extends ActiveRecord
  14. {
  15. public static function getDb()
  16. {
  17. return \Yii::$app->old_db;
  18. }
  19. public static function tableName()
  20. {
  21. return "{{%comments}}";
  22. }
  23. public function getPull()
  24. {
  25. return $this->hasOne(CommentsPull::class,['comment_pull_id'=>'id']);
  26. }
  27. public function getOldPost()
  28. {
  29. return $this->hasOne(OldNews::class,['comment_pull_id'=>'comment_pull_id']);
  30. }
  31. public function getPost()
  32. {
  33. return $this->hasOne(News::class,['uid'=>'id'])->via('oldPost');
  34. }
  35. public function getPublishedAt(){
  36. if($this->created_at<date("Y-m-d H:i:s",strtotime("-1 day"))){
  37. return date("d",strtotime($this->created_at))." ".mb_strtolower(Transliterator::month(date("n",strtotime($this->created_at))));
  38. } else {
  39. $diff = ceil((time() - strtotime($this->created_at))/60); //В минутах
  40. if($diff<=60){
  41. return Transliterator::plural($diff,['минуту','минуты', 'минут'],true,'только что','минуту')." назад";
  42. } else {
  43. $diff = (int)floor($diff/60);
  44. return Transliterator::plural($diff,['час','часа', 'часов'],true,'только что','час')." назад";
  45. }
  46. }
  47. }
  48. }