top-think / top-think/think-orm
Conversion 的 toArray优化/bug
Nobody has claimed this yet.
- Dominant language
- PHP
- Stars
- 437
- Forks
- 188
- PR merge metrics
- No merged PRs in 30d
Description
重现代码:
$goods = GoodsModel::with($with)->find($goods_id);
$goods['goods_detail_media'] = $goods['goods_media']->where('from', 4)->values();
$goods['goods_media'] = $goods['goods_media']->where('from', 1)->values();
我在上面的with关联查询中,查询了goods_media关联数据,查询出来以后,我再将goods_media数据进行拆分,将from 1和4分别放到不同的属性中,但是goods['goods_media'],在toArray输出goods对象之前,我发现返回的goods['goods_media']数据是对的。这个属性在toArray输出后,返回的还是关联数据,并不是我过滤后的数据
查询代码发现在trait Conversion文件的toArray函数中,
$data = array_merge($this->data,$this->relation);
这导致relation的数据覆盖了我data中的数据,就导致了我的赋值不生效
改为
$data = array_merge($this->relation,$this->data);
后,解决了我的问题
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in the Conversion trait's toArray function and inspect how $data and $relation are merged. Reproduce the GoodsModel relation example, then verify that the filtered goods_media values remain in the toArray output and that the existing behavior is covered or confirmed by the relevant ORM tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- database
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100