think-orm icon indicating copy to clipboard operation
think-orm copied to clipboard

多对多关联 - 关联预载入时回调操作”function($query)“后数据异常

Open xhwsd opened this issue 4 years ago • 0 comments

测试代码:

/**
 * 测试1 - 不做任何验证
 */
public function test1()
{
	// 查询列表
	$query = AftersaleModel::with([
		// 销售明细
		'sales' => function($query) {
			// // 为前端售后单详细提供支持...
			// $query->with([
			// 	'products' => function($query) {
			// 		$query->with(['product.specification']);
			// 		// 售价、金额
			// 		//$query->append(['price', 'amount']);
			// 	}
			// ]);
			$query->append([
				// 产品数量、产品剩余、产品金额
				'products_quantity', 'products_remainder', 'products_amount',
				// 收款有效金额
				'receivables_valid_amount'
			]);
		}
	]);
	$query->order('create_time', 'desc');
	$list = $query->select();

	// 响应成功
	success($list->toArray());
}

多对多关联定义

/**
 * 销售 - 该售后关联的销售列表
 * 
 * @return BelongsToMany 正向多对多关联
 */
public function sales()
{
	return $this->belongsToMany(Sale::class, AftersaleSales::class, 'sale_id', 'aftersale_id');
}

开启排序响应 $query->order('create_time', 'desc');

{
    "code": 0,
    "msg": "成功",
    "data": [
        {
            "id": 57,
            "shop_id": 1,
            "client_id": 67,
            "reason": 1,
            "liability": 1,
            "method_type": "exchange",
            "method_id": 46,
            "remark": "333",
            "user_id": 2,
            "status": 1,
            "payment_id": 0,
            "create_time": "2021-09-02 19:51:59",
            "update_time": "2021-09-02 19:51:59",
            "sales": []
        },
        {
            "id": 56,
            "shop_id": 1,
            "client_id": 67,
            "reason": 1,
            "liability": 1,
            "method_type": "exchange",
            "method_id": 45,
            "remark": "333",
            "user_id": 2,
            "status": 1,
            "payment_id": 0,
            "create_time": "2021-09-02 18:59:00",
            "update_time": "2021-09-02 18:59:00",
            "sales": [
                {
                    "id": 17,
                    "shop_id": 1,
                    "client_id": 67,
                    "nickname": "test1",
                    "name": "test1",
                    "phone": "13000000000",
                    "region_id": "340203001",
                    "region_detailed": "安徽省芜湖市弋江区弋江桥街道",
                    "address": "test1",
                    "source": 1,
                    "settlement": 1,
                    "deduct": 0,
                    "message": "",
                    "flag": 0,
                    "qrder_time": "2021-08-23 20:20:06",
                    "invoice_info": null,
                    "invoice_open": false,
                    "remark": "",
                    "user_id": 2,
                    "status": 4,
                    "nickname_full": "test1",
                    "nickname_abbr": "",
                    "name_full": "test1",
                    "name_abbr": "",
                    "create_time": "2021-08-23 20:20:07",
                    "update_time": "2021-08-28 16:33:52",
                    "pivot": {
                        "id": 70,
                        "aftersale_id": 56,
                        "sale_id": 17,
                        "create_time": "2021-09-02 18:59:00",
                        "update_time": "2021-09-02 18:59:00"
                    },
                    "products_quantity": 40,
                    "products_remainder": 32,
                    "products_amount": 330,
                    "receivables_valid_amount": 97
                },
                {
                    "id": 18,
                    "shop_id": 1,
                    "client_id": 67,
                    "nickname": "test1",
                    "name": "test1",
                    "phone": "13000000000",
                    "region_id": "340203001",
                    "region_detailed": "安徽省芜湖市弋江区弋江桥街道",
                    "address": "test1",
                    "source": 1,
                    "settlement": 1,
                    "deduct": 0,
                    "message": "",
                    "flag": 0,
                    "qrder_time": "2021-08-23 21:04:11",
                    "invoice_info": null,
                    "invoice_open": false,
                    "remark": "",
                    "user_id": 2,
                    "status": 4,
                    "nickname_full": "test1",
                    "nickname_abbr": "",
                    "name_full": "test1",
                    "name_abbr": "",
                    "create_time": "2021-08-23 21:04:12",
                    "update_time": "2021-09-01 12:20:50",
                    "pivot": {
                        "id": 72,
                        "aftersale_id": 56,
                        "sale_id": 18,
                        "create_time": "2021-09-02 19:40:45",
                        "update_time": "2021-09-02 19:40:51"
                    },
                    "products_quantity": 40,
                    "products_remainder": 40,
                    "products_amount": 230,
                    "receivables_valid_amount": 100
                }
            ]
        }
    ]
}

未开启排序响应 $query->order('create_time', 'desc');

{
    "code": 0,
    "msg": "成功",
    "data": [
        {
            "id": 56,
            "shop_id": 1,
            "client_id": 67,
            "reason": 1,
            "liability": 1,
            "method_type": "exchange",
            "method_id": 45,
            "remark": "333",
            "user_id": 2,
            "status": 1,
            "payment_id": 0,
            "create_time": "2021-09-02 18:59:00",
            "update_time": "2021-09-02 18:59:00",
            "sales": []
        },
        {
            "id": 57,
            "shop_id": 1,
            "client_id": 67,
            "reason": 1,
            "liability": 1,
            "method_type": "exchange",
            "method_id": 46,
            "remark": "333",
            "user_id": 2,
            "status": 1,
            "payment_id": 0,
            "create_time": "2021-09-02 19:51:59",
            "update_time": "2021-09-02 19:51:59",
            "sales": [
                {
                    "id": 18,
                    "shop_id": 1,
                    "client_id": 67,
                    "nickname": "test1",
                    "name": "test1",
                    "phone": "13000000000",
                    "region_id": "340203001",
                    "region_detailed": "安徽省芜湖市弋江区弋江桥街道",
                    "address": "test1",
                    "source": 1,
                    "settlement": 1,
                    "deduct": 0,
                    "message": "",
                    "flag": 0,
                    "qrder_time": "2021-08-23 21:04:11",
                    "invoice_info": null,
                    "invoice_open": false,
                    "remark": "",
                    "user_id": 2,
                    "status": 4,
                    "nickname_full": "test1",
                    "nickname_abbr": "",
                    "name_full": "test1",
                    "name_abbr": "",
                    "create_time": "2021-08-23 21:04:12",
                    "update_time": "2021-09-01 12:20:50",
                    "pivot": {
                        "id": 73,
                        "aftersale_id": 57,
                        "sale_id": 18,
                        "create_time": "2021-09-02 19:51:59",
                        "update_time": "2021-09-02 19:51:59"
                    },
                    "products_quantity": 40,
                    "products_remainder": 40,
                    "products_amount": 230,
                    "receivables_valid_amount": 100
                }
            ]
        }
    ]
}

描述

通过屏蔽排序代码$query->order('create_time', 'desc');,测试发现 data[0].sales 为空数组(异常),而data[1].sales 正常。 比对从侧面验证关联定义和数据是正常的,否则排序和不排序都应该无法查询出关联数据

xhwsd avatar Sep 02 '21 12:09 xhwsd