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

2018-03-23 15:24 更新

查詢數(shù)據(jù)實(shí)際上已經(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] ]) ]); } }

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

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

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

/**

  • 渲染分頁(yè)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() ); } } }

二、服務(wù)層

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


use app\common\service\ServiceBase;
use \tpfcore\Core;
/**
 * 基礎(chǔ)服務(wù)
 */
class User extends FrontendBase
{
    public function editUser($data){
        // 在進(jìn)行數(shù)據(jù)操作前進(jìn)行數(shù)據(jù)驗(yàn)證
        $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)計(jì)數(shù)據(jù)

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

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

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

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

得到某個(gè)字段的值

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

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

5、getList($param=[])

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

注意:只有通過(guò)此查詢的數(shù)據(jù)才有可能進(jìn)行數(shù)據(jù)緩存,默認(rèn)參數(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 ]

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)