catfan / Medoo

The lightweight PHP database framework to accelerate the development.
https://medoo.in
MIT License
4.83k stars 1.15k forks source link

Data mapping bug #1007

Closed xrayffa closed 3 years ago

xrayffa commented 3 years ago

$data = $medoo->select('user', ['id', 'name']) 结果正常: [{"id":"1","name":"张三"}]

$data = $medoo->select('user', ['id', '映射'=> ['name']]); 结果正常: [{"id":"1","映射":{"name":"张三"}}]

$data = $medoo->select('user', [ '映射'=> ['name']]); 结果报错: Fatal error: Uncaught InvalidArgumentException: Incorrect column name "映射" in 。。。。

$data = $medoo->select('user', [ 'name'=> ['name']]); 结果异常: {"张三":{"name":"张三"} 怎么把字段的值作为键名了?

$data = $medoo->select('user', [ 'userData'=> ['name']]); 结果为空!

$data = $medoo->select('user', ['id(随便加一个存在的字段名)', 'userData'=> ['name']]); 结果正常: [{"id":"1","userData":{"name":"张三"}}] 但这样岂不是达不到数据分组的目的?

catfan commented 3 years ago

That's because you are using Index Mapping not Data Mapping that the key: [array] as the first value of column parameter.

https://medoo.in/api/select (#Index Mapping section)

The way to solve this problem:

  1. Add more columns as possible.
  2. Just use it without data mapping, because there is no need for that with only one mapping.
    // What is this struct for in the real production environment?
    [
    {
        "userData": ["foo"]
    },
    {
        "userData": ["bar"]
    }
    ]
  3. Or create a new empty stack array, and push any value fetched from the database as the mapping struct whatever you want.