amdp3-metaforums/Application/Models/Post.php

38 lines
1.3 KiB
PHP
Raw Normal View History

2019-11-19 03:36:39 +00:00
<?php
namespace Application\Models;
use Application\Foundations\Model as DBModel;
use Application\Foundations\DateHelper;
use Application\Foundations\QueryBuilder;
use Application\Services\ServiceContainer;
class Post extends DBModel {
public function user() {
2019-11-20 05:19:02 +00:00
$user = User::find($this->user_id);
2019-11-19 03:36:39 +00:00
return $user;
}
public function elapsed_created_attribute() {
return DateHelper::elapsedString($this->created_at);
}
public function favorites_attribute() {
$query = new QueryBuilder();
$query = $query->select("COUNT(user_id) AS count")->from("userfavorite")->where("post_id",$this->id)->build();
$result = ServiceContainer::Database()->select($query);
return $result[0]["count"];
}
2019-11-20 05:19:02 +00:00
public function thread() {
$thread = Thread::find($this->thread_id);
return $thread;
}
2019-11-19 03:36:39 +00:00
public function is_main() {
$id = Thread::find($this->thread_id)->main_post->id;
return ($id == $this->id);
}
2019-11-23 08:58:59 +00:00
public function is_favorited() {
$query = new QueryBuilder();
$query = $query->where('user_id',ServiceContainer::Authentication()->user()->id)->where('post_id',$this->id);
$is_fav = UserFavorite::selectOne($query);
return isset($is_fav);
}
2019-11-19 03:36:39 +00:00
}