CodeIgniter Forums
Active Record: Problems using SQL's replace with comma inside a Select statement - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Active Record: Problems using SQL's replace with comma inside a Select statement (/showthread.php?tid=44419)



Active Record: Problems using SQL's replace with comma inside a Select statement - El Forum - 08-13-2011

[eluser]Marcelo Reborn[/eluser]
(Sorry about the confusing title, I couldn't make it better)
- There's a big explaning -

This is my select statement

Code:
$select = '
id,
idc,
CONCAT( "R$ ", REPLACE(valor, ".", ",")) AS val';
...
$this->db->select($select);
...

This is the generated query string (note that a space "born" after the comma, inside the replace)
Code:
SELECT id, idc, CONCAT( "R$ ", REPLACE(valor,".",", ")) AS val,
Then, a monetary value that should be converted from 1.50 to R$1,50, was converted to R$1, 50.


I already figured out that this is caused by the Active Record class, which explodes all commas, and later, implodes them all with an space after:
Code:
$sql .= implode(', ', $this->ar_select);
(DB_active_rec.php, line 1512)


- Conclusion -
I just removed that space from implode() and my monetary values came back to normal.
I just want to know if CI crew would consider this a bug, because that spaces after the commas into an sql query are just "decoration".



Best regards from Brazil.


Active Record: Problems using SQL's replace with comma inside a Select statement - El Forum - 08-14-2011

[eluser]Aken[/eluser]
It does this for the purpose of escaping parts of the query, usually the column names. I wouldn't consider this a bug so much as a limitation of Active Record.

I'd suggest either extending the Active Record class and create your own select() function, or skip Active Record entirely and write out your queries then use the query() function.


Active Record: Problems using SQL's replace with comma inside a Select statement - El Forum - 08-14-2011

[eluser]InsiteFX[/eluser]
You cannot extend the codeigniter database class, you have to modify them.
This is in the User Guide.

InsiteFX


Active Record: Problems using SQL's replace with comma inside a Select statement - El Forum - 08-14-2011

[eluser]Marcelo Reborn[/eluser]
As I said before, I solved my problem changing DB_active_rec.php.
I just created this post to report that problem.


Active Record: Problems using SQL's replace with comma inside a Select statement - El Forum - 08-14-2011

[eluser]Aken[/eluser]
[quote author="InsiteFX" date="1313333603"]You cannot extend the codeigniter database class, you have to modify them.
This is in the User Guide.

InsiteFX[/quote]
You can extend the Loader class to "teach" CI to look for extended DB files as well. There's a lot of ways to do a lot of things Smile