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

一对多关联并求值排序

**

  • 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}
                    ]
                ]
            });
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

孙奋斗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值