Skip to content

Commit 8bae53b

Browse files
committed
docs: improve explanation about Model pagination
1 parent 0068d20 commit 8bae53b

3 files changed

Lines changed: 47 additions & 5 deletions

File tree

user_guide_src/source/libraries/pagination.rst

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,34 @@ The first element is the results from the database, **users**, which is retrieve
3939
the Model will hold on to the instance it used and store it in the public property, ``$pager``. So, we grab
4040
that and assign it to the ``$pager`` variable in the view.
4141

42-
.. important:: It is important to understand that the ``Model::paginate()`` method uses the **Model** and **QueryBuilder** methods.
43-
Therefore, trying to use ``$db->query()`` and ``Model::paginate()`` **will not work** because ``$db->query()`` executes
44-
the query immediately and is not associated with a QueryBuilder.
42+
Customizing Query for Pagination
43+
================================
4544

46-
To define conditions for pagination in a model, you can:
45+
To customize a query for pagination in a model, you can add
46+
:doc:`Query Builder <../database/query_builder>` methods before ``paginate()``
47+
method.
48+
49+
To add WHERE conditions:
4750

4851
.. literalinclude:: pagination/003.php
52+
:lines: 2-
53+
54+
You can join another table:
55+
56+
.. literalinclude:: pagination/016.php
57+
:lines: 2-
58+
59+
.. important:: It is important to understand that the ``Model::paginate()`` method
60+
uses the **Model** and the **Query Builder** instance in the Model.
61+
Therefore, trying to use ``Model::paginate()`` with :ref:`db-query`
62+
**will not work** because ``$db->query()`` executes the query immediately
63+
and is not associated with the Query Builder.
64+
65+
If you need a complicated SQL query that you cannot write with Query Builder,
66+
try using :ref:`db-query` and `Manual Pagination`_.
67+
68+
Displaying Pager Links
69+
======================
4970

5071
Within the view, we then need to tell it where to display the resulting links::
5172

user_guide_src/source/libraries/pagination/003.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
];
1010

1111
// You can move the conditions to a separate method.
12-
// Model method
1312
class UserModel extends Model
1413
{
14+
// ...
15+
1516
public function banned()
1617
{
1718
$this->builder()->where('ban', 1);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
class NewsModel extends Model
4+
{
5+
protected $table = 'news';
6+
7+
// ...
8+
9+
public function getPagination(?int $perPage = null): array
10+
{
11+
$this->builder()
12+
->select('news.*, category.name')
13+
->join('category', 'news.category_id = category.id');
14+
15+
return [
16+
'news' => $this->paginate($perPage),
17+
'pager' => $this->pager,
18+
];
19+
}
20+
}

0 commit comments

Comments
 (0)