Skip to content

Commit d45bd46

Browse files
authored
Merge pull request #7581 from kenjis/docs-model-pagination
docs: improve Paginating with Models
2 parents c68af0e + 041d2bc commit d45bd46

9 files changed

Lines changed: 103 additions & 33 deletions

File tree

user_guide_src/source/database/queries.rst

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,16 @@ Query Basics
1515
Regular Queries
1616
===============
1717

18-
To submit a query, use the **query** function:
18+
.. _db-query:
19+
20+
$db->query()
21+
------------
22+
23+
To submit a query, use the ``query()`` method:
1924

2025
.. literalinclude:: queries/001.php
2126

22-
The ``query()`` function returns a database result **object** when "read"
27+
The ``query()`` method returns a database result **object** when "read"
2328
type queries are run which you can use to :doc:`show your
2429
results <results>`. When "write" type queries are run it simply
2530
returns true or false depending on success or failure. When retrieving
@@ -34,6 +39,11 @@ this:
3439
Simplified Queries
3540
==================
3641

42+
.. _db-simplequery:
43+
44+
$db->simpleQuery()
45+
------------------
46+
3747
The ``simpleQuery()`` method is a simplified version of the
3848
``$db->query()`` method. It DOES
3949
NOT return a database result set, nor does it set the query timer, or

user_guide_src/source/libraries/pagination.rst

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,46 @@ 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+
Adding WHERE
50+
------------
51+
52+
If you want to add WHERE conditions, you can specify conditions directly:
4753

4854
.. literalinclude:: pagination/003.php
55+
:lines: 2-
56+
57+
You can move the conditions to a separate method:
58+
59+
.. literalinclude:: pagination/017.php
60+
61+
.. literalinclude:: pagination/018.php
62+
:lines: 2-
63+
64+
Adding JOIN
65+
-----------
66+
67+
You can join another table:
68+
69+
.. literalinclude:: pagination/016.php
70+
71+
.. important:: It is important to understand that the ``Model::paginate()`` method
72+
uses the **Model** and the **Query Builder** instance in the Model.
73+
Therefore, trying to use ``Model::paginate()`` with :ref:`db-query`
74+
**will not work** because ``$db->query()`` executes the query immediately
75+
and is not associated with the Query Builder.
76+
77+
If you need a complicated SQL query that you cannot write with Query Builder,
78+
try using :ref:`db-query` and `Manual Pagination`_.
79+
80+
Displaying Pager Links
81+
======================
4982

5083
Within the view, we then need to tell it where to display the resulting links::
5184

user_guide_src/source/libraries/pagination/002.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
namespace App\Controllers;
44

5-
use CodeIgniter\Controller;
6-
7-
class UserController extends Controller
5+
class UserController extends BaseController
86
{
97
public function index()
108
{
Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,9 @@
11
<?php
22

3-
// You can specify conditions directly.
3+
// In your Controller.
44
$model = new \App\Models\UserModel();
55

66
$data = [
77
'users' => $model->where('ban', 1)->paginate(10),
88
'pager' => $model->pager,
99
];
10-
11-
// You can move the conditions to a separate method.
12-
// Model method
13-
class UserModel extends Model
14-
{
15-
public function banned()
16-
{
17-
$this->builder()->where('ban', 1);
18-
19-
return $this; // This will allow the call chain to be used.
20-
}
21-
}
22-
23-
$data = [
24-
'users' => $model->banned()->paginate(10),
25-
'pager' => $model->pager,
26-
];

user_guide_src/source/libraries/pagination/004.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
namespace App\Controllers;
44

5-
use CodeIgniter\Controller;
6-
7-
class UserController extends Controller
5+
class UserController extends BaseController
86
{
97
public function index()
108
{

user_guide_src/source/libraries/pagination/015.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
namespace App\Controllers;
44

5-
use CodeIgniter\Controller;
6-
7-
class UserController extends Controller
5+
class UserController extends BaseController
86
{
97
public function index()
108
{
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use CodeIgniter\Model;
6+
7+
class NewsModel extends Model
8+
{
9+
protected $table = 'news';
10+
11+
// ...
12+
13+
public function getPagination(?int $perPage = null): array
14+
{
15+
$this->builder()
16+
->select('news.*, category.name')
17+
->join('category', 'news.category_id = category.id');
18+
19+
return [
20+
'news' => $this->paginate($perPage),
21+
'pager' => $this->pager,
22+
];
23+
}
24+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use CodeIgniter\Model;
6+
7+
class UserModel extends Model
8+
{
9+
// ...
10+
11+
public function banned()
12+
{
13+
$this->builder()->where('ban', 1);
14+
15+
return $this; // This will allow the call chain to be used.
16+
}
17+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
// In your Controller.
4+
$model = new \App\Models\UserModel();
5+
6+
$data = [
7+
'users' => $model->banned()->paginate(10),
8+
'pager' => $model->pager,
9+
];

0 commit comments

Comments
 (0)