easy-swoole / orm

31 stars 27 forks source link

orm快速查询indexBy返回结果错误 #206

Closed shaoyuyuan closed 3 years ago

shaoyuyuan commented 3 years ago

easyswoole框架版本号、orm组件版本号 [Version]

easyswoole v3.4.4 orm dev-master

问题描述和截图 [Question]

$res = BannerModel::create()->indexBy('state'); 返回的结果与官方文档不一致 image

实际使用返回了索引结果为最后一条数据

{
  "code": 200,
  "result": {
    "1": {
      "bannerId": 2,
      "bannerName": "测试banner2",
      "bannerImg": "23423.jpg",
      "bannerDescription": "测试2",
      "bannerUrl": null,
      "state": 1
    }
  },
  "msg": null
}

排查情况和最小复现脚本 [Tests and Recurrence]

    $res = BannerModel::create()->indexBy('state');

    $this->writeJson(200, $res);

错误原因

EasySwoole\ORM\Db\Result中的getResultIndexBy方法错误
line:124 现在是$indexedModels[$model[$column]] = $model;
应该改为$indexedModels[$model[$column]][] = $model;

测试结果:

{
  "code": 200,
  "result": {
    "1": [
      {
        "bannerId": 1,
        "bannerName": "测试banner",
        "bannerImg": "asdadsasdasd.jpg",
        "bannerDescription": "测试的banner数据",
        "bannerUrl": "www.php20.cn",
        "state": 1
      },
      {
        "bannerId": 2,
        "bannerName": "测试banner2",
        "bannerImg": "23423.jpg",
        "bannerDescription": "测试2",
        "bannerUrl": null,
        "state": 1
      }
    ]
  },
  "msg": null
}
Player626 commented 3 years ago

你自己使用方式有问题 你的 state 都为1

shaoyuyuan commented 3 years ago

就因为state都为1所以结果应该是 key的值应该是个数组吗? 难道说这个方法只支持唯一索引?

Player626 commented 3 years ago

indexBy 从字面意义不就是 索引以某个字段特定值来作为的嘛

Player626 commented 3 years ago

同等于array_column函数

Player626 commented 3 years ago

去qq群找下我