解决Collection Object ([:protected]=Array())报错

今天用laravel的DB取多个表的数据做统计运算,里面使用了多次DB查询并嵌套三个for循环、多个->leftJoin()一次使用、->select()多键,结果只能取到数据中的第一条数据,从第二条开始的数组格式数据都为:

Illuminate\Support\Collection Object ( [items:protected] => Array ( ) ) 

而且PHP、laravel都不会报错。

很明显,数组:protected 以后,数组运算的“键=>值”都会为空。为空也不报错,很蛋疼。

百度后没找到原因,于是我逐步打印每次数据运算的结果,最后排查到某地方在运算

->leftJoin()

的时候,数据出现了:protected ,可以才想到可能是多余的多个leftJoin差生了某种错误。

于是,简化了和删除了多个->leftJoin(),同时简化了->select()里面的键,果然解决以上问题。

不知道是不是触发了PHP里面的安全校验,还是触发了MySQL里面的校验,进而造成了数组出现:protected 。

-

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
解释一下这段代码def price(request): if request.method == "GET": contact_list = models.zufang.objects.values_list().order_by('id') contact_list = [i for i in contact_list] new_contact_list = random.sample(contact_list, 3) user_name = models.Collection.objects.get(name=request.session.get('user_name')) if user_name.count > 0: collection = user_name.collection collection = collection.split(",") collection = [int(i) for i in collection] find_index = [] for item in collection: find_index.append(models.zufang.objects.values_list().filter(id=item)[0]) find_index = [i for i in find_index] else: collection = [] find_index = [] if request.GET.get("update_item") == "ok": return JsonResponse({"new_contact_list": new_contact_list, "find_index": find_index}, safe=False) return render(request, 'price.html', locals()) else: user_name = models.Collection.objects.get(name=request.session.get('user_name')) count = user_name.count if user_name.count > 0: collection = user_name.collection collection = collection.split(",") else: collection = [] if request.POST.get("new") == "true": count += 1 new_collection = request.POST.get("data_id") collection.append(new_collection) collection = ','.join(collection) new = models.Collection.objects.get(name=request.session.get('user_name')) new.collection = collection new.count = count new.save() else: count -= 1 new_collection = request.POST.get("data_id") collection.remove(new_collection) collection = ','.join(collection) new = models.Collection.objects.get(name=request.session.get('user_name')) new.collection = collection new.count = count new.save() return JsonResponse({}, safe=False)
04-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值