Files
zyt/admin/BUGFIX_ORDERBY.md
2026-03-11 09:49:47 +08:00

4.4 KiB

订单系统 Bug 修复

问题描述

错误信息:

Typed property app\\common\\lists\\BaseDataLists::$orderBy must not be accessed before initialization

原因分析

OrderLists 类继承自 BaseDataLists,但没有实现 ListsSearchInterface 接口。

BaseDataListsinitSort() 方法中,只有当类实现了 ListsSortInterface 接口时,才会初始化 $orderBy 属性:

private function initSort()
{
    if (!($this instanceof ListsSortInterface)) {
        return [];  // 如果没有实现接口,$orderBy 不会被初始化
    }
    // ...
}

lists() 方法尝试使用 $this->orderBy 时,由于属性未初始化,就会抛出错误。

解决方案

修改 OrderLists 类

  1. 改变继承关系:从 BaseDataLists 改为 BaseAdminDataLists
  2. 实现接口:实现 ListsSearchInterface 接口
  3. 实现方法:实现 setSearch() 方法定义搜索条件
  4. 移除 orderBy 使用:使用固定的排序而不是 $this->orderBy

修改后的代码

<?php

namespace app\adminapi\lists\order;

use app\adminapi\lists\BaseAdminDataLists;
use app\common\lists\ListsSearchInterface;
use app\common\model\Order;

class OrderLists extends BaseAdminDataLists implements ListsSearchInterface
{
    /**
     * @notes 设置搜索条件
     * @return array
     */
    public function setSearch(): array
    {
        return [
            '=' => ['order_type', 'status'],
            'like' => ['order_no'],
        ];
    }

    /**
     * @notes 获取列表
     * @return array
     */
    public function lists(): array
    {
        $where = $this->searchWhere;
        
        // 处理患者关键词搜索
        if (!empty($this->params['patient_keyword'])) {
            $where[] = ['patient_id', 'in', function ($query) {
                $query->table('user')
                    ->where('nickname|mobile', 'like', '%' . $this->params['patient_keyword'] . '%')
                    ->field('id');
            }];
        }
        
        // 处理创建时间范围
        if (!empty($this->params['create_time_start'])) {
            $where[] = ['create_time', '>=', $this->params['create_time_start'] . ' 00:00:00'];
        }
        
        if (!empty($this->params['create_time_end'])) {
            $where[] = ['create_time', '<=', $this->params['create_time_end'] . ' 23:59:59'];
        }
        
        return Order::where($where)
            ->with(['patient', 'creator', 'details'])
            ->order(['create_time' => 'desc'])  // 使用固定排序
            ->limit($this->limitOffset, $this->pageSize)
            ->select()
            ->toArray();
    }

    /**
     * @notes 获取数量
     * @return int
     */
    public function count(): int
    {
        $where = $this->searchWhere;
        
        if (!empty($this->params['patient_keyword'])) {
            $where[] = ['patient_id', 'in', function ($query) {
                $query->table('user')
                    ->where('nickname|mobile', 'like', '%' . $this->params['patient_keyword'] . '%')
                    ->field('id');
            }];
        }
        
        if (!empty($this->params['create_time_start'])) {
            $where[] = ['create_time', '>=', $this->params['create_time_start'] . ' 00:00:00'];
        }
        
        if (!empty($this->params['create_time_end'])) {
            $where[] = ['create_time', '<=', $this->params['create_time_end'] . ' 23:59:59'];
        }
        
        return Order::where($where)->count();
    }
}

关键改动

项目 原来 修改后
基类 BaseDataLists BaseAdminDataLists
接口 ListsSearchInterface
搜索条件 getSearchWhere() 方法 setSearch() 方法
排序 $this->orderBy ['create_time' => 'desc']

验证修复

修复后,访问订单列表 API 应该能正常工作:

curl -X GET "http://localhost:8000/order.order/lists" \
  -H "token: your_admin_token"

预期响应:

{
  "code": 1,
  "msg": "success",
  "data": {
    "lists": [],
    "count": 0,
    "page_no": 1,
    "page_size": 15
  }
}

相关文件

  • server/app/adminapi/lists/order/OrderLists.php - 已修复

参考

  • server/app/adminapi/lists/doctor/RosterLists.php - 参考实现
  • server/app/common/lists/BaseDataLists.php - 基类实现

修复日期: 2024-03-10 状态: 完成