Codeigniter 4 默认分页使用模型 Class 没有顺序和加入
Code Igniter 4 Default pagination using Model Class has no order by and Join
$postModel = new \App\Models\PostModel();
$pager = \Config\Services::pager();
$post = $postModel->orderBy('dteCreatedDate', 'DESC')->findAll();
$data = [
'post' => $postModel->paginate(2,'post'),
'pager' => $postModel->pager
];
我有上面的代码可以在我的 code igniter 4 项目中创建一个 simpleLink 分页。此分页有效,但缺少 1 条信息和对结果的排序。
我需要 select 来自另一个 table 的列,该列连接到 PostModel 中的 table。
如何向 $postModel
添加连接和排序依据,以便我可以获得所需的所有数据并对结果集进行排序。
提供给模型 Class 的 pagination()
的结果默认是分页的,这就是我想使用此功能的原因
如何将连接和排序依据添加到模型 Class 默认 CRUD
分页似乎直接作用于模型中声明的 table,即在 protected $table = 'table_name'.
中声明的内容
所以我在想,如果你需要在一个或多个 table 上使用 JOIN 和其他一些东西,"a way" 围绕这个,就是创建一个 VIEW table .
我试了一下这个并想出了一些工作代码。
这是相当微不足道的,但它似乎证明了这一点。
我有两个 table。 (非常松散地基于 Cluedo 和睡眠不足)
Table 1
table_1
id,
name,
type
with inserted data of
1, Fred, Baker
2, Sam , Candle Stick Maker
Table 2
table_2
id,
place
with inserted data of
1, Laundry
2, Bathroom
在 PostModel 中我有
protected $table = 'table_view';
/**
* Only need to create the View so the pagination can access it via
* $this->table (protected $table = 'table_view')
*/
public function create_view() {
$sql = "CREATE OR REPLACE VIEW table_view AS ";
// Whatever your SQL needs to be goes here
$sql .= "SELECT t1.name, t1.type, t2.place FROM table_2 t2
JOIN table_1 t1 on t1.id = t2.table_1_id";
echo $sql;
$query = $this->db->query($sql);
}
然后你的分页方法可以变成
public function index() {
$postModel = new \App\Models\PostModel();
$postModel->create_view();
$pager = \Config\Services::pager();
$data = [
'posts' => $postModel->paginate(2),
'pager' => $postModel->pager
];
echo view('users_view', $data);
}
我的观点是
<h2> The results</h2>
<?php
echo '<pre>';
echo 'LINE: ' . __LINE__ . ' Module ' . __CLASS__ . '<br>';
var_dump($posts);
echo '</pre>';
?>
<table>
<?php foreach ($posts as $post): ?>
<tr>
<td><?= $post['name']; ?></td>
<td><?= $post['type']; ?></td>
<td><?= $post['place']; ?></td>
</tr>
<?php endforeach; ?>
</table>
它给出了(我没有包括分页但我确实测试过)的输出
CREATE OR REPLACE VIEW table_view AS SELECT t1.name, t1.type, t2.place FROM table_2 t2 JOIN table_1 t1 on t1.id = t2.table_1_id
The results
LINE: 10 Module
array(2) {
[0]=>
array(3) {
["name"]=>
string(4) "Fred"
["type"]=>
string(5) "Baker"
["place"]=>
string(7) "Laundry"
}
[1]=>
array(3) {
["name"]=>
string(3) "Sam"
["type"]=>
string(18) "Candle Stick Maker"
["place"]=>
string(8) "Bathroom"
}
}
Fred Baker Laundry
Sam Candle Stick Maker Bathroom
"appears" table 名称,在本例中是一个不存在的 table(直到它被创建)不会扰乱模型的 $table被设置为不存在的 table.
总结
创建模型
声明 $table 为您的视图名称。
创建一个创建(或替换)视图的方法。
这大约是第一次创建它和随后的更新。
调用创建视图的Model方法
在您的分页中使用模型。 (它现在指向视图)。
这可能不是最好的主意,但它有点符合它想要的工作方式。
我试过"standard"的方法,但他们不想玩好。
我还使用了最基本的 CI SQL 功能,如果您愿意,可以使用构建器等。
我希望这能给你一些想法。
$postModel = new \App\Models\PostModel();
$pager = \Config\Services::pager();
$post = $postModel->orderBy('dteCreatedDate', 'DESC')->findAll();
$data = [
'post' => $postModel->paginate(2,'post'),
'pager' => $postModel->pager
];
我有上面的代码可以在我的 code igniter 4 项目中创建一个 simpleLink 分页。此分页有效,但缺少 1 条信息和对结果的排序。
我需要 select 来自另一个 table 的列,该列连接到 PostModel 中的 table。
如何向 $postModel
添加连接和排序依据,以便我可以获得所需的所有数据并对结果集进行排序。
提供给模型 Class 的 pagination()
的结果默认是分页的,这就是我想使用此功能的原因
如何将连接和排序依据添加到模型 Class 默认 CRUD
分页似乎直接作用于模型中声明的 table,即在 protected $table = 'table_name'.
中声明的内容所以我在想,如果你需要在一个或多个 table 上使用 JOIN 和其他一些东西,"a way" 围绕这个,就是创建一个 VIEW table .
我试了一下这个并想出了一些工作代码。 这是相当微不足道的,但它似乎证明了这一点。
我有两个 table。 (非常松散地基于 Cluedo 和睡眠不足)
Table 1
table_1
id,
name,
type
with inserted data of
1, Fred, Baker
2, Sam , Candle Stick Maker
Table 2
table_2
id,
place
with inserted data of
1, Laundry
2, Bathroom
在 PostModel 中我有
protected $table = 'table_view';
/**
* Only need to create the View so the pagination can access it via
* $this->table (protected $table = 'table_view')
*/
public function create_view() {
$sql = "CREATE OR REPLACE VIEW table_view AS ";
// Whatever your SQL needs to be goes here
$sql .= "SELECT t1.name, t1.type, t2.place FROM table_2 t2
JOIN table_1 t1 on t1.id = t2.table_1_id";
echo $sql;
$query = $this->db->query($sql);
}
然后你的分页方法可以变成
public function index() {
$postModel = new \App\Models\PostModel();
$postModel->create_view();
$pager = \Config\Services::pager();
$data = [
'posts' => $postModel->paginate(2),
'pager' => $postModel->pager
];
echo view('users_view', $data);
}
我的观点是
<h2> The results</h2>
<?php
echo '<pre>';
echo 'LINE: ' . __LINE__ . ' Module ' . __CLASS__ . '<br>';
var_dump($posts);
echo '</pre>';
?>
<table>
<?php foreach ($posts as $post): ?>
<tr>
<td><?= $post['name']; ?></td>
<td><?= $post['type']; ?></td>
<td><?= $post['place']; ?></td>
</tr>
<?php endforeach; ?>
</table>
它给出了(我没有包括分页但我确实测试过)的输出
CREATE OR REPLACE VIEW table_view AS SELECT t1.name, t1.type, t2.place FROM table_2 t2 JOIN table_1 t1 on t1.id = t2.table_1_id
The results
LINE: 10 Module
array(2) {
[0]=>
array(3) {
["name"]=>
string(4) "Fred"
["type"]=>
string(5) "Baker"
["place"]=>
string(7) "Laundry"
}
[1]=>
array(3) {
["name"]=>
string(3) "Sam"
["type"]=>
string(18) "Candle Stick Maker"
["place"]=>
string(8) "Bathroom"
}
}
Fred Baker Laundry
Sam Candle Stick Maker Bathroom
"appears" table 名称,在本例中是一个不存在的 table(直到它被创建)不会扰乱模型的 $table被设置为不存在的 table.
总结
创建模型
声明 $table 为您的视图名称。
创建一个创建(或替换)视图的方法。 这大约是第一次创建它和随后的更新。
调用创建视图的Model方法
在您的分页中使用模型。 (它现在指向视图)。
这可能不是最好的主意,但它有点符合它想要的工作方式。
我试过"standard"的方法,但他们不想玩好。 我还使用了最基本的 CI SQL 功能,如果您愿意,可以使用构建器等。
我希望这能给你一些想法。