1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-06-24 04:01:31 +08:00
2021-05-20 18:50:50 +08:00

105 lines
2.7 KiB
PHP

<?php
namespace App\Repos;
use App\Library\Paginator\Adapter\QueryBuilder as PagerQueryBuilder;
use App\Models\Comment as CommentModel;
use Phalcon\Mvc\Model;
use Phalcon\Mvc\Model\Resultset;
use Phalcon\Mvc\Model\ResultsetInterface;
class Comment extends Repository
{
public function paginate($where = [], $sort = 'latest', $page = 1, $limit = 15)
{
$builder = $this->modelsManager->createBuilder();
$builder->from(CommentModel::class);
$builder->where('1 = 1');
if (!empty($where['id'])) {
$builder->andWhere('id = :id:', ['id' => $where['id']]);
}
if (!empty($where['owner_id'])) {
$builder->andWhere('owner_id = :owner_id:', ['owner_id' => $where['owner_id']]);
}
if (!empty($where['item_id'])) {
$builder->andWhere('item_id = :item_id:', ['item_id' => $where['item_id']]);
}
if (!empty($where['item_type'])) {
$builder->andWhere('item_type = :item_type:', ['item_type' => $where['item_type']]);
}
if (isset($where['parent_id'])) {
$builder->andWhere('parent_id = :parent_id:', ['parent_id' => $where['parent_id']]);
}
if (isset($where['published'])) {
$builder->andWhere('published = :published:', ['published' => $where['published']]);
}
if (isset($where['deleted'])) {
$builder->andWhere('deleted = :deleted:', ['deleted' => $where['deleted']]);
}
if ($sort == 'reported') {
$builder->andWhere('report_count > 0');
}
switch ($sort) {
case 'popular':
$orderBy = 'like_count DESC';
break;
default:
$orderBy = 'id DESC';
break;
}
$builder->orderBy($orderBy);
$pager = new PagerQueryBuilder([
'builder' => $builder,
'page' => $page,
'limit' => $limit,
]);
return $pager->paginate();
}
/**
* @param int $id
* @return CommentModel|Model|bool
*/
public function findById($id)
{
return CommentModel::findFirst([
'conditions' => 'id = :id:',
'bind' => ['id' => $id],
]);
}
/**
* @param array $ids
* @param array|string $columns
* @return ResultsetInterface|Resultset|CommentModel[]
*/
public function findByIds($ids, $columns = '*')
{
return CommentModel::query()
->columns($columns)
->inWhere('id', $ids)
->execute();
}
public function countComments()
{
return (int)CommentModel::count(['conditions' => 'deleted = 0']);
}
}