Skip to content

Commit 041d2bc

Browse files
committed
docs: improve sample code and description
1 parent 30bda4f commit 041d2bc

5 files changed

Lines changed: 45 additions & 21 deletions

File tree

user_guide_src/source/libraries/pagination.rst

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,27 @@ To customize a query for pagination in a model, you can add
4646
:doc:`Query Builder <../database/query_builder>` methods before ``paginate()``
4747
method.
4848

49-
To add WHERE conditions:
49+
Adding WHERE
50+
------------
51+
52+
If you want to add WHERE conditions, you can specify conditions directly:
5053

5154
.. literalinclude:: pagination/003.php
5255
:lines: 2-
5356

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+
5467
You can join another table:
5568

5669
.. literalinclude:: pagination/016.php
57-
:lines: 2-
5870

5971
.. important:: It is important to understand that the ``Model::paginate()`` method
6072
uses the **Model** and the **Query Builder** instance in the Model.
Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +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-
class UserModel extends Model
13-
{
14-
// ...
15-
16-
public function banned()
17-
{
18-
$this->builder()->where('ban', 1);
19-
20-
return $this; // This will allow the call chain to be used.
21-
}
22-
}
23-
24-
$data = [
25-
'users' => $model->banned()->paginate(10),
26-
'pager' => $model->pager,
27-
];

user_guide_src/source/libraries/pagination/016.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<?php
22

3+
namespace App\Models;
4+
5+
use CodeIgniter\Model;
6+
37
class NewsModel extends Model
48
{
59
protected $table = 'news';
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)