mirror of
https://gitee.com/koogua/course-tencent-cloud.git
synced 2025-06-22 03:32:47 +08:00
77 lines
1.8 KiB
PHP
77 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Repos;
|
|
|
|
use App\Library\Paginator\Adapter\QueryBuilder as PagerQueryBuilder;
|
|
use App\Models\Page as PageModel;
|
|
use Phalcon\Mvc\Model;
|
|
use Phalcon\Mvc\Model\Resultset;
|
|
use Phalcon\Mvc\Model\ResultsetInterface;
|
|
|
|
class Page extends Repository
|
|
{
|
|
|
|
public function paginate($where = [], $sort = 'latest', $page = 1, $limit = 15)
|
|
{
|
|
$builder = $this->modelsManager->createBuilder();
|
|
|
|
$builder->from(PageModel::class);
|
|
|
|
$builder->where('1 = 1');
|
|
|
|
if (!empty($where['title'])) {
|
|
$builder->andWhere('title LIKE :title:', ['title' => "%{$where['title']}%"]);
|
|
}
|
|
|
|
if (isset($where['published'])) {
|
|
$builder->andWhere('published = :published:', ['published' => $where['published']]);
|
|
}
|
|
|
|
if (isset($where['deleted'])) {
|
|
$builder->andWhere('deleted = :deleted:', ['deleted' => $where['deleted']]);
|
|
}
|
|
|
|
switch ($sort) {
|
|
default:
|
|
$orderBy = 'id DESC';
|
|
break;
|
|
}
|
|
|
|
$builder->orderBy($orderBy);
|
|
|
|
$pager = new PagerQueryBuilder([
|
|
'builder' => $builder,
|
|
'page' => $page,
|
|
'limit' => $limit,
|
|
]);
|
|
|
|
return $pager->paginate();
|
|
}
|
|
|
|
/**
|
|
* @param int $id
|
|
* @return PageModel|Model|bool
|
|
*/
|
|
public function findById($id)
|
|
{
|
|
return PageModel::findFirst([
|
|
'conditions' => 'id = :id:',
|
|
'bind' => ['id' => $id],
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @param array $ids
|
|
* @param array|string $columns
|
|
* @return ResultsetInterface|Resultset|PageModel[]
|
|
*/
|
|
public function findByIds($ids, $columns = '*')
|
|
{
|
|
return PageModel::query()
|
|
->columns($columns)
|
|
->inWhere('id', $ids)
|
|
->execute();
|
|
}
|
|
|
|
}
|