tpframe之查詢數(shù)據(jù)

2018-03-23 15:24 更新

查詢數(shù)據(jù)實際上已經(jīng)有了

一、控制器

在控制器里面添加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] ]) ]); } }

就這么簡單就實現(xiàn)了查詢的操作,那分頁怎么辦呢?不急,直接在視圖里面須要顯示分頁的地方加入{$list->render()}就可以了

注意這里要實現(xiàn)分頁,必須傳遞paginate參數(shù),并指定每頁顯示多少條

具體實現(xiàn)用的是Bootstrap類里面的render方法實現(xiàn)的

/**

  • 渲染分頁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){
        // 在進行數(shù)據(jù)操作前進行數(shù)據(jù)驗證
        $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')

聚合函數(shù)-統(tǒng)計數(shù)據(jù)

例如:self::getStatistics(["status"=>0]); //統(tǒng)計正常用戶有多少

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

得到某個列的數(shù)組

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

得到某個字段的值

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

查找單條記錄,返回的是一維數(shù)組

5、getList($param=[])

獲取數(shù)據(jù)

注意:只有通過此查詢的數(shù)據(jù)才有可能進行數(shù)據(jù)緩存,默認參數(shù)如下

[ "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 ]

以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號