Files
zyt/REGION_DATABASE_SAVE_FIX.md
T
2026-04-16 11:13:26 +08:00

5.1 KiB

Region Database Save Fix (省市区数据库保存修复)

Issue

The region fields (province, city, district) were being sent in the API request payload but were not being saved to the database.

Root Cause Analysis

Frontend Issue

The frontend was sending the wrong parameter names:

  • Sent: province, city, district
  • Expected by backend: shipping_province, shipping_city, shipping_district

Backend Issue

The create() method in PrescriptionOrderLogic.php was not handling the province/city/district fields at all, even though the edit() method had the code to handle them.

Changes Made

1. Frontend - Fixed Parameter Names

File: admin/src/views/consumer/prescription/index.vue

Changed the payload to use the correct parameter names:

// Before:
payload.province = createOrderForm.region[0]
payload.city = createOrderForm.region[1]
payload.district = createOrderForm.region[2]

// After:
payload.shipping_province = createOrderForm.region[0]
payload.shipping_city = createOrderForm.region[1]
payload.shipping_district = createOrderForm.region[2]

2. Backend - Added Region Fields to create() Method

File: server/app/adminapi/logic/tcm/PrescriptionOrderLogic.php

Added code to handle province/city/district fields in the create() method:

// 处理省市区字段
if (isset($params['shipping_province'])) {
    $order->shipping_province = (string) $params['shipping_province'];
}
if (isset($params['shipping_city'])) {
    $order->shipping_city = (string) $params['shipping_city'];
}
if (isset($params['shipping_district'])) {
    $order->shipping_district = (string) $params['shipping_district'];
}

This matches the existing code in the edit() method.

Data Flow (After Fix)

Create Order Flow:

  1. User selects region: 北京市 → 北京市 → 丰台区
  2. Frontend stores: createOrderForm.region = ['北京市', '北京市', '丰台区']
  3. Frontend sends payload:
    {
      "shipping_province": "北京市",
      "shipping_city": "北京市",
      "shipping_district": "丰台区",
      ...other fields
    }
    
  4. Backend create() method processes:
    $order->shipping_province = "北京市";
    $order->shipping_city = "北京市";
    $order->shipping_district = "丰台区";
    
  5. Database columns are populated:
    shipping_province: 北京市
    shipping_city: 北京市
    shipping_district: 丰台区
    

Testing Steps

1. Test Create Order with Region

  1. Open prescription list page
  2. Click "创建订单" on any prescription
  3. Fill in required fields:
    • Select patient
    • Enter recipient name and phone
    • Select region: 北京市 → 北京市 → 丰台区
    • Enter detailed address
  4. Click "创建业务订单"
  5. Check browser Network tab:
    • Verify payload includes:
      {
        "shipping_province": "北京市",
        "shipping_city": "北京市",
        "shipping_district": "丰台区"
      }
      

2. Verify Database

After creating the order, check the database:

SELECT 
    id, 
    order_no, 
    shipping_province, 
    shipping_city, 
    shipping_district, 
    shipping_address 
FROM tcm_prescription_order 
ORDER BY id DESC 
LIMIT 1;

Expected result:

| id | order_no | shipping_province | shipping_city | shipping_district | shipping_address |
|----|----------|-------------------|---------------|-------------------|------------------|
| XX | POXXXXXX | 北京市            | 北京市        | 丰台区            | 顶顶顶           |

3. Test Different Regions

Test with various regions to ensure all work correctly:

  • 四川省 → 成都市 → 武侯区
  • 上海市 → 上海市 → 浦东新区
  • 广东省 → 深圳市 → 南山区

4. Test Edit Order

  1. Edit an existing order
  2. Change the region
  3. Save and verify the database is updated

Database Schema

The database should have these columns (from add_shipping_region_fields.sql):

ALTER TABLE `tcm_prescription_order` 
ADD COLUMN `shipping_province` VARCHAR(50) DEFAULT NULL COMMENT '收货省份' AFTER `shipping_address`,
ADD COLUMN `shipping_city` VARCHAR(50) DEFAULT NULL COMMENT '收货城市' AFTER `shipping_province`,
ADD COLUMN `shipping_district` VARCHAR(50) DEFAULT NULL COMMENT '收货区县' AFTER `shipping_city`;

Frontend:

  • admin/src/views/consumer/prescription/index.vue
    • Changed parameter names in submitCreateOrderFromPrescription()

Backend:

  • server/app/adminapi/logic/tcm/PrescriptionOrderLogic.php
    • Added province/city/district handling in create() method
    • Already had province/city/district handling in edit() method

Database:

  • add_shipping_region_fields.sql (migration file)

Summary

Fixed frontend parameter names: provinceshipping_province, etc.
Added province/city/district handling to backend create() method
Backend edit() method already had the correct code
Database columns will now be populated correctly on order creation
Database columns will be updated correctly on order edit

The region fields are now properly saved to the database for both create and edit operations!