tpframe之查询数据

查询数据实际上已经有了

一、控制器

在控制器里面添加index方法

<?php
namespace app\frontend\controller;
use \tpfcore\Core;
class User extends FrontendBase
{
public function add()
{
IS_POST && $this->jump(Core::loadModel($this->name)->saveUser($this->param));
return $this->fetch("add");
}
public function edit(){
IS_POST && $this->jump(Core::loadModel($this->name)->editUser($this->param));
return $this->fetch("edit",[
"list"=>Core::loadModel($this->name)->listUser($this->param)
]);
}
public function del(){
$this->jump(Core::loadModel($this->name)->delUser($this->param));
}
public function index(){
return $this->fetch("index",[
"list"=>Core::loadModel($this->name)->listUser([
"order"=>"id desc"
"paginate" => ['rows' => 30]
])
]);
}

}

就这么简单就实现了查询的操作,那分页怎么办呢?不急,直接在视图里面须要显示分页的地方加入{$list->render()}就可以了

注意这里要实现分页,必须传递paginate参数,并指定每页显示多少条

具体实现用的是Bootstrap类里面的render方法实现的

/**
* 渲染分页html
* @return mixed
*/
public function render()
{
if ($this->hasPages()) {
if ($this->simple) {
return sprintf(
'<ul class="pager">%s %s</ul>',
$this->getPreviousButton(),
$this->getNextButton()
);
} else {
return sprintf(
'<ul class="pagination">%s %s %s</ul>',
$this->getPreviousButton(),
$this->getLinks(),
$this->getNextButton()
);
}
}
}

二、服务层

<?php
// +----------------------------------------------------------------------
// | Author: yaoyihong <510974211@qq.com>
// +----------------------------------------------------------------------
namespace app\frontend\service;

use app\common\service\ServiceBase;
use \tpfcore\Core;
/**
 * 基础服务
 */
class User extends FrontendBase
{
	public function editUser($data){
		// 在进行数据操作前进行数据验证
		$validate=\think\Loader::validate($this->name);
		$validate_result = $validate->scene('edit')->check($data);
        if (!$validate_result) {    
            return [RESULT_ERROR, $validate->getError(), null];
        }
        return Core::loadModel($this->name)->saveObject($data);
	}
	public function listUser($data){
		return Core::loadModel($this->name)->listUser($data);
	}
}

三、查询的其它操作

1、getStatistics ($where = [], $stat_type = 'count', $field = 'id')

聚合函数-统计数据

例如:self::getStatistics(["status"=>0]); //统计正常用户有多少

2、array getColumns($where = [], $field = '', $key = '')

得到某个列的数组

3、mixed getColumnValue($where = [], $field = '', $default = null, $force = false)

得到某个字段的值

4、getOneObject($where = [], $field = true)

查找单条记录,返回的是一维数组

5、getList($param=[])

获取数据

注意:只有通过此查询的数据才有可能进行数据缓存,默认参数如下

	[
            "where" =>[],
            "field" =>true,
            "order" =>"",
            "paginate"  =>['rows' => null, 'simple' => false, 'config' => []],
            "join"      =>['join' => null, 'condition' => null, 'type' => 'INNER'],
            "group"     =>['group' => '', 'having' => ''],
            "limit" =>null,
            "data"  =>null,
            "expire"=>0
	]
爆料早知道: