CodeIgniter Forums
Using functions in selects with DataMapper OZ - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: Using functions in selects with DataMapper OZ (/showthread.php?tid=36355)



Using functions in selects with DataMapper OZ - El Forum - 11-29-2010

[eluser]macron[/eluser]
My problem is that I want to format the ISO date used by MySQL using the DATE_FORMAT mysql function. I'm aware of the ActiveRecord bug as described in the DataMapper documentation but this does not work:
Code:
$n->new News();
$n->select("id,headline,dk,DATE_FORMAT(date,'%e %M %Y')")->order_by('date', 'desc')->limit(3)->get();
Why?
I wan't something like this to come out of the query:
Code:
SELECT `id`, `headline`, `dk`, DATE_FORMAT(date, '%e %M %Y') FROM `news` ORDER BY `news`.`date` desc LIMIT 3

What am I doing wrong here?


Using functions in selects with DataMapper OZ - El Forum - 11-29-2010

[eluser]macron[/eluser]
Argh. Sorry for the newbee q. here. Found the answer: adding a FALSE as a second param. to make the select not quote the mysql function


Using functions in selects with DataMapper OZ - El Forum - 11-29-2010

[eluser]WanWizard[/eluser]
See the manual on how to deal with functions in Datamapper:
Code:
$n->select("id,headline,dk")->select_func("DATE_FORMAT", array('@date', '[,]', "'%e %M %Y'"), 'date')->order_by('date', 'desc')->get();
For security reasons, you don't want to disable escaping if other options are available.


Using functions in selects with DataMapper OZ - El Forum - 11-29-2010

[eluser]macron[/eluser]
Thanks.
Whats the '[,]' in the array?


Using functions in selects with DataMapper OZ - El Forum - 11-29-2010

[eluser]WanWizard[/eluser]
Have you looked at the manual (I've included the link)? The third bullet at the top says it all...


Using functions in selects with DataMapper OZ - El Forum - 11-29-2010

[eluser]macron[/eluser]
Hi
Yes. I get it. It's a raw string. But why is the formatting string (%e...) not in square brackets or the comma not in double-quotes. Why the diff.?


Using functions in selects with DataMapper OZ - El Forum - 11-29-2010

[eluser]WanWizard[/eluser]
The formatting string is a string, enclosed in quotes. Anything enclosed in quotes is passed without escaping, so the brackets aren't needed. The comma can't be enclosed in quotes, yet you need to prevent it from being escaped.