如何使用查询构建器(最佳方法)在phalcon中实现此查询:
$results = "select * from table where name like 'A%' limit 10"; // <-- 10 records
$total = "select count(1) from table where name like 'A%'"; // <-- 100 records
return [
'data' => $result,
'total' => $total
];
im extjs用户,我需要获得总超限以显示分页信息(例如:从100条记录中显示1到10)
THX。
答案 0 :(得分:3)
您可以使用Pagination
实现此目的,请参阅此处:Pagination Docs
唯一的事就是使用QueryBuilder
适配器。但这可以通过以下方式完成:
$builder = $this->modelsManager->createBuilder()
->from('Table')
->andWhere('name like :name:', array('name' => 'A%' ) );
$paginator = new Phalcon\Paginator\Adapter\QueryBuilder(array(
"builder" => $builder,
"limit"=> 10,
"page" => 1
));
return [
'data' => $paginator->items,
'total' => $paginator->total_items
];