config.xml
<database> <default driver="mysql" host="tfmysql" port="3306" username="root" password="abcdef" database="test" charset="utf8mb4"></default> </database>
model/user.inc.php
class user extends tfmodel{
public function __construct(tfphp $tfphp){
$tableUser = new dao\user($tfphp);
$tableUserDetail = new dao\userDetail($tfphp);
parent::__construct($tfphp, [
"user"=>new tfdaoOneToOne($tfphp, [
$tableUser,
$tableUserDetail
], [
"fieldMapping"=>[["userId"=>"userId"]]
])
]);
}
public function getUsersWithPages(): array{
$ds = $this->tfphp->getDataSource();
$pp = 2;
$cp = ($_GET["pn"]) ? $_GET["pn"] : 1;
// process
$sql = "select * from user u
inner join userDetail ud
on u.userId = ud.userId
order by u.userId desc";
$totalUsers = $ds->fetchOne3("select count(*) as cc from (". preg_replace("/select.*from/i", "select 1 from", $sql). ") as tt", []);
if($totalUsers === null){
return [];
}
$pages = $ds->makePagination($totalUsers["cc"], $pp, $cp);
$users = $ds->fetchMany3($sql, [], ($cp-1)*$pp, $pp);
if($users === null){
return [];
}
return [
"data"=>$users,
"page"=>$pages,
];
}
}controller/usersWithPages.inc.php
class usersWithPages extends tfpage {
protected function onLoad(){
$user = new user($this->tfphp);
$users = $user->getUsersWithPages();
$this->view->setVar("users", $users);
}
}view/usersWithPages.html
<h2>user list with pages</h2> <div> <table border="1"> <thead> <tr> <th>ID</th> <th>name</th> <th>state</th> <th>create time</th> <th>update time</th> </tr> </thead> <tbody> <% if $users.data %> <% for $user in $users.data %> <tr> <td><% $user.userId %></td> <td><% $user.userName %></td> <td><% $user.state %></td> <td><% $user.createDT %></td> <td><% $user.updateDT %></td> </tr> <% /for %> <tr> <td colspan="5"> <% if $users.page.links.first > 0 %><a href="usersWithPages">first</a><% /if %> <% if $users.page.links.previous > 0 %><a href="usersWithPages?pn=<% $users.page.links.previous %>">previous</a><% /if %> <% if $users.page.links.next > 0 %><a href="usersWithPages?pn=<% $users.page.links.next %>">next</a><% /if %> <% if $users.page.links.last > 0 %><a href="usersWithPages?pn=<% $users.page.links.last %>">last</a><% /if %> </td> </tr> <% else %> <tr> <td colspan="5"> user records are not found </td> </tr> <% /if %> </tbody> </table> </div>
有数据的情况

没有数据的情况
