lampager / lampager-cakephp2
为 CakePHP 2 提供快速分页
Requires
- php: ^5.6 || ^7.0
- composer/installers: *
- lampager/lampager: ^0.4
Requires (Dev)
- cakephp/cakephp: ^2.10
- php-coveralls/php-coveralls: ^2.2
- phpunit/phpunit: <6.0.0
README
Lampager for CakePHP 2
不使用 OFFSET 的快速分页
要求
- PHP: ^5.6 || ^7.0
- CakePHP: ^2.10
- lampager/lampager: ^0.4
注意
- 对于 CakePHP 2.x,使用 lampager/lampager-cakephp2(此版本)。
- 对于 CakePHP 3.x,使用 lampager/lampager-cakephp v1.x。
- 对于 CakePHP 4.x,使用 lampager/lampager-cakephp v2.x。
安装
composer require lampager/lampager-cakephp2
如果需要,将 Plugin/Lampager
移动到适当的目录。
基本用法
作为插件加载。有关详细信息,请参阅 如何安装插件。
插件需要在 app/Config/bootstrap.php
中手动加载
// Be sure to require vendor/autoload.php beforehand. // CakePlugin::load() will fail unless autoloader is properly configured. CakePlugin::load('Lampager');
接下来,将 'Lampager.Lampager'
添加到您的模型类中(建议使用 AppModel
)
class AppModel extends Model { public $actsAs = [ 'Lampager.Lampager', ]; }
在以下方法之一中使用
- 在控制器中使用(通过
LampagerBehavior
) - 在模型中使用(通过
LampagerBehavior
)
在控制器中使用
首先,您的 Model
类必须启用 'Lampager.Lampager'
。使用方式请参考食谱: 分页。注意 Lampager 的特定选项,如 forward
、seekable
或 cursor
。
/** @var \Lampager\PaginationResult $posts */ $posts = $this->paginate(Post::class, [ // Lampager options 'forward' => true, 'seekable' => true, 'cursor' => [ 'Post' => [ 'id' => '4', 'created' => '2017-01-01 10:00:00', ], ], // PaginatorComponent::settings query 'conditions' => [ 'Post.type' => 'public', ], 'order' => [ 'Post.created' => 'DESC', 'Post.id' => 'DESC', ], 'limit' => 10, ]); $this->set('posts', $posts);
在模型中使用
首先,您的 Model
类必须启用 'Lampager.Lampager'
。只需使用 Model::find
并传入 lampager
。自定义查找类型 lampager
(见 检索数据)的工作方式类似于核心查找类型 all
,并附加了额外的参数和后处理程序。
/** @var \Lampager\PaginationResult $posts */ $posts = $this->find('lampager', [ // Lampager options 'forward' => true, 'seekable' => true, 'cursor' => [ 'Post' => [ 'id' => '4', 'created' => '2017-01-01 10:00:00', ], ], // Model::find query 'limit' => 10, 'order' => [ 'Post.modified' => 'DESC', 'Post.created' => 'DESC', 'Post.id' => 'DESC', ], ]); foreach ($posts as $post) { /** @var mixed[][] $post */ debug($post['Post']['id']); debug($post['Post']['created']); debug($post['Post']['modified']); }
类
另请参阅: lampager/lampager。
API
另请参阅: lampager/lampager。
建议使用 Model::find()
或 PaginatorComponent::paginate()
。查询将与 CakePHP 查询合并并传递到 Lampager\Query
。
LampagerPaginator::__construct()
LampagerPaginator::create()
创建一个新的分页器实例。这些方法不打算直接在您的代码中使用。
static LampagerPaginator::create(Model $builder, array $options): static LampagerPaginator::__construct(Model $builder, array $options)
LampagerPaginator::transform()
将 Lampager 查询转换为 CakePHP 查询。
LampagerPaginator::transform(\Lampager\Query $query): array
LampagerPaginator::build()
执行配置 + 转换。
LampagerPaginator::build(array $cursor = []): array
LampagerPaginator::paginate()
执行配置 + 转换 + 处理。
LampagerPaginator::paginate(array $cursor = []): \Lampager\PaginationResult
参数
(array)
$cursor
一个关联数组,包含$column => $value
。它必须是 全部或无。- 对于初始页面,省略此参数或传递一个空数组。
- 对于后续页面,传递所有参数。不允许部分参数。
返回值
例如:
(使用 Model::find()
的默认格式)
object(Lampager\PaginationResult)#1 (5) { ["records"]=> array(3) { [0]=> array(1) { ["Post"]=> array(3) { ... } } [1]=> array(1) { ["Post"]=> array(3) { ... } } [2]=> array(1) { ["Post"]=> array(3) { ... } } } ["hasPrevious"]=> bool(false) ["previousCursor"]=> NULL ["hasNext"]=> bool(true) ["nextCursor"]=> array(1) { ["Post"]=> array(2) { ["id"]=> string(1) "3" ["created"]=> string(19) "2017-01-01 10:00:00" } } }
LampagerTransformer::__construct()
创建一个新的转换器实例。此类不打算直接在您的代码中使用。
LampagerTransformer::__construct(Model $builder, array $options)
示例
本节描述了 lampager-cakephp2 的实际用法。
在控制器中使用
以下示例展示了如何从请求中接受 cursor 参数并通过 PaginatorComponent::settings
传递它。确保您的 Model
类已启用 'Lampager.Lampager'
。
class PostsController extends AppController { public function index() { // Get cursor parameters $previous = $this->request->param('named.previous_cursor'); $next = $this->request->param('named.next_cursor'); $this->Paginator->settings = [ // Lampager options // If the previous_cursor is not set, paginate forward; otherwise backward 'forward' => !$previous, 'cursor' => $previous ?: $next ?: [], 'seekable' => true, // PaginatorComponent::settings query 'conditions' => [ 'Post.type' => 'public', ], 'order' => [ 'Post.created' => 'DESC', 'Post.id' => 'DESC', ], 'limit' => 10, ]; /** @var \Lampager\PaginationResult $posts */ $posts = $this->Paginator->paginate(Post::class); $this->set('posts', $posts); } }
分页链接可以输出如下
// If there is a previous page, print pagination link if ($posts->hasPrevious) { echo $this->Html->link('<< Previous', [ 'controller' => 'posts', 'action' => 'index', 'previous_cursor' => $posts->previousCursor, ]); } // If there is a next page, print pagination link if ($posts->hasNext) { echo $this->Html->link('Next >>', [ 'controller' => 'posts', 'action' => 'index', 'next_cursor' => $posts->nextCursor, ]); }
支持的数据库引擎
MySQL, MariaDB, PostgreSQL, 和 SQLite
支持!
Microsoft SQL Server
不支持。