catfan / Medoo

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

Setting type+rename field with Raw doesnt work on 1.5.7 #778

Closed ghost closed 6 years ago

ghost commented 6 years ago

Strangly enought... Renaming fields (using AS) doesnt work when using medoo::raw() in my query. I may forget something but i'm pretty sure about this !

example (work : all fields are in integer):

[
    "p.id(prov_id)[Int]",
    "tc[Int]"            => Medoo::raw('SUM(<tc>)'),
    "international[Int]" => Medoo::raw('SUM(<international>)'),
    "secondary[Int]"     => Medoo::raw('SUM(<secondary>)'),
    "postsecondary[Int]" => Medoo::raw('SUM(<postsecondary>)'),
]

But this doesnt (tc/international/secondary/postsecondary remain string AND original name) :

[
    "p.id(prov_id)[Int]",
    "tc(gr_tc)[Int]"            => Medoo::raw('SUM(<tc>)'),
    "international(gr_int)[Int]" => Medoo::raw('SUM(<international>)'),
    "secondary(gr_se)[Int]"     => Medoo::raw('SUM(<secondary>)'),
    "postsecondary(gr_ps)[Int]" => Medoo::raw('SUM(<postsecondary>)'),
]

It seems to not returning the proper type but always returning as string when using medoo:raw(). For now i must do a proper formatting with an additional forearch loop... annoying !

catfan commented 6 years ago

When you are using SUM(), AVG() or other aggregate functions with Medoo::raw object, there no need to using alias, because the tc, international, secondary and postsecondary will no longer present as table column.

You can just simply assign it as this:

[
    "p.id(prov_id)[Int]",
    "gr_tc [Int]"            => Medoo::raw('SUM(<tc>)'),
    "gr_int [Int]" => Medoo::raw('SUM(<international>)'),
    "gr_se [Int]"     => Medoo::raw('SUM(<secondary>)'),
    "gr_ps [Int]" => Medoo::raw('SUM(<postsecondary>)'),
]
ghost commented 6 years ago

That makes sense... thx a lot, workin' perfectly !