iis服务器助手广告
返回顶部
首页 > 资讯 > 后端开发 > PHP编程 >tp5 fastadmin hasmany一对多关联并求值排序
  • 886
分享到

tp5 fastadmin hasmany一对多关联并求值排序

php开发语言 2023-09-30 16:09:48 886人浏览 八月长安
摘要

一对多关联并求值排序 ** Query.PHP 添加//thinkphp\library\think\db\Query.php ** //统计求和public function withSum($re

一对多关联并求值排序

**

  • Query.PHP 添加//thinkphp\library\think\db\Query.php

**

//统计求和public function withSum($relation, $subQuery = true, $sum='sum'){    if (!$subQuery) {        $this->options['with_sum'] = $relation;    } else {        $relations = is_string($relation) ? explode(',', $relation) : $relation;        if (!isset($this->options['field'])) {            $this->field('*');        }        foreach ($relations as $key => $relation) {            $closure = false;            if ($relation instanceof \Closure) {                $closure  = $relation;                $relation = $key;            }            $relation = Loader::parseName($relation, 1, false);            $count    = '(' . $this->model->$relation()->getRelationSuMQuery($closure, $sum) . ')';            $this->field([$count => Loader::parseName($relation) . '_sum']);        }    }    return $this;}

HasMany.php 添加://thinkphp\library\think\model\relation\HasMany.php

public function getRelationSumQuery($closure, $sum)    {        if ($closure) {            call_user_func_array($closure, [ & $this->query]);        }        $localKey = $this->localKey ?: $this->parent->getPk();        return $this->query->whereExp($this->foreignKey, '=' . $this->parent->getTable() . '.' . $localKey)->fetchsql()->sum($sum);    }

控制器添加调用

  public function index()    {        //当前是否为关联查询        $this->relationSearch = false;        //设置过滤方法        $this->request->filter(['strip_tags', 'trim']);        if ($this->request->isajax()) {            //如果发送的来源是Selectpage,则转发到Selectpage            if ($this->request->request('keyField')) {                return $this->selectpage();            }            list($where, $sort, $order, $offset, $limit) = $this->buildparams();                        $list =$this->model          ->withSum('ceshi1',true, 'num')//关联获取总值                    ->where($where)                    ->order($sort, $order)                    ->paginate($limit);             $result = array("total" => $list->total(), "rows" => $list->items());            return JSON($result);        }        return $this->view->fetch();    }

在对应model里面添加

 public function ceshi1(){        return $this->hasMany('ceshi1','ceshi_id');    }

在对应js里面添加

 table.bootstrapTable({                url: $.fn.bootstrapTable.defaults.extend.index_url,                pk: 'id',                sortName: 'ceshi1_sum',//排序的字段                 sortOrder: 'desc',//倒叙                columns: [                    [                        {checkbox: true},                        {field: 'id', title: __('Id')},                        {field: 'name', title: __('Name'), operate: 'LIKE'},                        {field: 'ceshi1_sum', title: __('Num')},                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, fORMatter: Table.api.formatter.operate}                    ]                ]            });

来源地址:https://blog.csdn.net/qq_42958118/article/details/127641476

--结束END--

本文标题: tp5 fastadmin hasmany一对多关联并求值排序

本文链接: https://www.lsjlt.com/news/421963.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作